﻿//*************************************************************************
//面向服务的协作式智能交通系统仿真软件SoC-ITSS v2.1 alpha
//发布单位：西北工业大学--巴黎高科MINES机器人与群智能系统联合实验室
//团队负责人：张凯龙 Zhang Kailong
//团队成员：张大方、杨安胜、王敏、谢策、王雨佳、谢尘玉等
//联系我们：kl.zhang@nwpu.edu.cn
//2012-2021
//*************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using CyberCarSimulation.Cloud;
using CyberCarSimulation.Bean;
using CyberCarSimulation.RoadModel;
using CyberCarSimulation.Simulation;
using CyberCarSimulation.Regulation;
using CyberCarSimulation.Algorithm;
                                                                                                                                                                                                            

//*******************************************************
//
//文件名(File Name):           Vehicle.cs
//
//数据表(Tables):              nothing 
//
//          修改理由：         交通云通信
//
//*******************************************************
namespace CyberCarSimulation.Regulation
{
    class Vehicle
    {
        //动力学特性
        public static float MAX_SPEED;                   //最大速度
        public static float ACCELERATION;                //最大加速度 5m/s^2
        public static float DECCELERATION;               //最大减速度
        public static float CRUISE_SPEED;                //巡航速度
        public static float CROSSING_SPEED;              //通过速度
        public static float SAFE_DISTANCE = 1f;          // 0.4m	deltaC = 0.4m  路口之前停止的距离
        public static float FRONT_SAFE_DISTANCE = 2.0f;  // 0.4m	deltaC = 0.4m 后车与前车之间的安全距离
        public static float MAX_SPEED_TAIL_SAFE_DISTANCE = (float)Math.Round((float)Math.Abs(Vehicle.MAX_SPEED * Vehicle.MAX_SPEED * 0.50f / Vehicle.DECCELERATION) + Vehicle.FRONT_SAFE_DISTANCE, Definitions.Precise); // deltaC' = 0.4m	db = 2.88m

        //车辆的物理属性
        public float CarLENGTH;                          // 3.6m（小车）  4.5大车
        public float CarWIDTH;                           // 1.5m（小车） 1.6大车
        public int CarType = Definitions.BigCar;         //车辆类型
        private String CarID;                            //车辆ID
        private readonly int Sche_Priority_Origin;       //最初的优先级，不可变动
        private float Act_Radius;                        //车辆的转弯半径，大车小车的转弯半径不同
        private int Act_Direction_Origin = 0;            //车辆的原来左右方向
        private String SCS_SectionID;                    //将要占用的车道序列        
        private int[] SCS_Array;                         //标志临界区申请的顺序
        private int SCS_Count;                           //车辆需申请的临界区的数量
        private float[] Coor_TurnPre_X;                  //车辆转弯前的X坐标
        private float[] Coor_TurnPre_Y;                  //车辆转弯前的Y坐标
        

        //为了兼容之前代码，这些车道都采用一位数字表示
        private int LaneIDOrigin;           //原车道      
        private int LaneIDDestination;      //目标车道
        private int LaneID;                 //现今所处的车道        
        private int LaneIDFinal;            //最终车道


        //描述起始点：起始路口、目标点：终止路口
        //原车道->当前车道->下一个车道->最终车道
        private string LaneID_Origin;                             //原车辆所在车道
        private string[] LaneID_Origin_Map = new string[2];       //当前车辆所在的车道图 第一个值为路口 第二个值为车道号

        private string LaneID_Current;                            //当前车辆所在车道
        private string[] LaneID_Current_Map = new string[2];      //当前车辆所在的车道图  第一个值为路口 第二个值为车道号

        private string LaneID_Previous;                           //之前车辆所在车道
        private string LaneID_Previous_Map;                       //之前车辆所在车道图   第一个值为路口 第二个值为车道号

        private string LaneID_Next;                               //下一步车辆需要到达的车道
        private string[] LaneID_Next_Map = new string[2];         //下一步车辆需要到达的车道图 第一个值为路口 第二个值为车道号

        private string LaneID_Final;                              //车辆最终的车道
        private string[] LaneID_Final_Map = new string[2];        //车辆最终的车道 第一个值为路口 第二个值为车道号

        private string[] VehiclePath = new string[100];           //车辆的路径 
        private bool AtTurnLaction = true;                        //记录在97位置设置nextLane

            
        //路口ID   路径规划使用
        private VehiclePathDijsktra_Sche VehiclePathDijsktra_Sche;  //车辆自主规划算法
        private string IntersectionID = null;        //当前所在路口ID
        private int IntersectionNumber = -1;         //路口ID序号

        private string IntersectionNextID = null;    //下一步所在路口ID
        private int IntersectionNextNumber = -1;     //下一步路口序号

        private string IntersectionFinalID = null;   //最终所在路口ID
        private int IntersectionFinalNumber = -1;    //最终路口序号

        private List<string> IntersectionPath = new List<string>();     //使用路口ID表示规划的路径
        private string PathNumber = "";              //路口序号

        private int PathPlaneTime = 0;



        //计算平均车速属性
        private double StartPointLocation;
        private int StertIntersectionNum;
        private double EndPontLocation;



        //车辆的前车属性
        private string Front_ID;            /*前车的ID（跟车时使用）*/
        private Vehicle FrontVehicle;       //前车
        private Vehicle FrontNewVehicle;    //换道之后的新的前车
        private bool FrontVehicleChanged;   //标识是否出现新的前车


        //第一个float用于存储当前行驶距离
        //float[]有两个元素，一：当前全局时间，二：车辆的速度
        private Dictionary<float, float[]> SpeedRecordQueue = null;     //每个TimeStep中记录当前车辆的速度，以备后续程序显示
        private float Act_Distance_Total;                               //[key] 车辆的所走的总路程
        private float Act_Speed_Current;                                //[0]全局时间 [1]车辆速度


        //车辆的时间
        private float GenerationTime;            //车辆的生成时间
        private float TimeInsert = 0.0f;         //车辆的插入时间
        private float TimePreRecord = 0.0f;      //上次记录速度时间
        private float TimeEnter = 0;             //车辆进入时间
        private float TimeReservation = 0;       //车辆预约时间
        private float TimeDelete = 0;            //车辆删除时间

        

        private int Sche_Priority_Current;       //当前的优先级，可变动*/
        private int Sche_Priority_Changed = 0;   //用于存储优先级，该优先级只提升，不减少，最终得到提升优先级的车辆数目
        private int State_Current;               //车辆所处状态


        /************************************************************************/
        /* 需改动                                                                     */
        /************************************************************************/
        
        
        private float Act_Acceleration_Current;         //加速度
        private float Act_Distance_Current;             //车辆的在一个时间片段中所走过的距离

        private float Act_Distance_Total_New = 0;           //在车里到达下一个路口时，重置之后
        private float Act_Distance_Total_Old = 0;           //在车里到达下一个路口时，重置之前，存放旧的车辆行驶总路程
        private float Act_Dis_Total_Correction_New = 0;     //在车里到达下一个路口时，重置之后
        private float Act_Dis_Total_Correction_Old = 0;     //在车里到达下一个路口时，重置之前，存放旧的车辆已目标车道为基准线的总路程 

        private float Act_Dis_Total_Correction;             //车辆已目标车道为基准线的总路程
        private float[] Coor_X;      //车辆的X坐标
        private float[] Coor_Y;      //车辆的Y坐标

        

        private int Act_Direction_Current;      //车辆当前方向
        private float Act_Radian_Current;       //当前转角
        private float Act_Radian_Total;         //当前转角

        private bool[] HasToken;                //标志车辆是否获得临界区
 

        //ts与te的存储方式，是按照通过临界区顺序大小，按照1、2、3的方式存储，而非是对照具体的临界区的时间
        private float[] TExpectedTs;            //理想到达各临界区的时间，空间为5，顺序为1-4
        private float[] TRealTs;             //实际到达各临界区的时间，空间为5，顺序为1-4
        private float[] TExpectedTe;            //理想离开各临界区的时间，空间为5，顺序为1-4
        private float[] TRealTe;             //实际离开各临界区的时间，空间为5，顺序为1-4
        private float[] TIdealTs; //车辆在无堵塞的情况下（最高速度）的情况下，到达各临界区的时间
        private float[] TIdealTe; //车辆在无堵塞的情况下（最高速度）的情况下，离开各临界区的时间

       

        private Queue<int> SCS_IDs_Leave;     //存储车辆需要使用的S-CS编号，
        //车辆通过某个S-CS后即将对应的数值从SCS_IDs_Leave中移除，
        //若SCS_IDs_Leave为空则表示车辆已经离开了路口。

        

        private Dictionary<float, float[]> Raise_Priority = new Dictionary<float, float[]>();   //其中元素分别是车辆提升优先级的时间、位置、
        
        private bool State_InLane = false;  //标识车辆是否已经处于监控区域之中

        private bool[] State_AlreadyTS;     //标识是否已经达到临界区
        private bool[] State_AlreadyTE;     //标识是否已经离开临界区
        private bool State_AlreadyLeave;    //标识是否已经离开监控区
        private int State_DeadLineMark;     //标志车辆现今处于哪一个deadline之前
        private int State_Turn = Definitions.PreTurn;   //标志车辆处于什么位置，选项有转弯前、转弯中、转完后
        private int State_Arrive = Definitions.ArrivePreSections;   //用于标识车辆现今已到达的范围
        private int State_Leave = Definitions.LeaveInit;    //标志车辆已离开的范围

        private bool sendMessageAllowed = false;    //标识是否允许发送预约消息

        private int Passed_ID = 0;      //已通过车辆的数目
        public bool State_IsDisUpadated = false;     //标志车辆是否已经按照当前的acc更新过total_dis等，即是否已经执行过this.computeDrivingDistance()
        //若已执行过，则标志为true，最终作为车辆与前车同一时间片下距离的判断标准
        private bool IsAuth = false;    //标志当前车辆是否已经被授权



        //对车辆的静态参数进行设置，以保证修改参数时，静态参数被修改
        public static void setParms()
        {
            Vehicle.MAX_SPEED = MainData.SimulationConfig.GetTxtMaximumSpeed();             //最大速度
            Vehicle.ACCELERATION = MainData.SimulationConfig.GetTxtMaximumAcceleration();   // 5m/s^2最大加速度
            Vehicle.DECCELERATION = MainData.SimulationConfig.GetTxtMaximumDeceleration();  //最大减速度
            Vehicle.CRUISE_SPEED = MainData.SimulationConfig.GetTxtCruisingSpeed();         //巡航速度
            Vehicle.CROSSING_SPEED = MainData.SimulationConfig.GetTxtCrossingSpeed();       //通过临界区速度
            Vehicle.MAX_SPEED_TAIL_SAFE_DISTANCE = (float)Math.Round((float)Math.Abs(Vehicle.MAX_SPEED * Vehicle.MAX_SPEED * 0.50f / Vehicle.DECCELERATION) + Vehicle.FRONT_SAFE_DISTANCE, Definitions.Precise); // deltaC' = 0.4m	db = 2.88m
        }

        //无参构造函数
        public Vehicle()
        { 
        }

        //有参构造函数
        public Vehicle( string LaneID_Origin, string LaneID_Final,string CarID,
                        int Priority, int CarType, float GenerationTime)
        {


            //Test
            //if (LaneID_Origin.Equals("4040511"))
            //{
            //    //this.VehiclePath = new string[] { "4040511", "4040141" };
            //    //this.VehiclePath = new string[] { "4040511", "4050611", "4060241" };
            //    this.VehiclePath = new string[] {"4040511","4050611","4061131","4111211","4121631" };
            //}
            //if (LaneID_Origin.Equals("4030731"))
            //{
            //    this.VehiclePath = new string[] { "4030731", "4071231", "4121631"};
            //}
            ////if (LaneID_Origin.Equals("4040511")) 
            ////{
            ////    this.VehiclePath = new string[] { "4040511", "4050611", "4060711", "4071231", "4121631" };
            ////}
            //if (LaneID_Origin.Equals("4020631"))
            //{
            //    this.VehiclePath = new string[] { "4020631", "4061131", "4111531"};
            //}
            //if (LaneID_Origin.Equals("4010531")) 
            //{
            //    this.VehiclePath = new string[] { "4010531", "4051031", "4101431" };
            //}
            //if (LaneID_Origin.Equals("4091011")) 
            //{
            //    this.VehiclePath = new string[] { "4091011", "4101111", "4111211", "4121311" };
            //} 
            
            //最先设置初始生成车道和终止车道
            this.LaneID_Origin = LaneID_Origin;      //车辆生成的原始车道
            this.LaneID_Final = LaneID_Final;        //车辆最终到达车道
            this.LaneID_Current = LaneID_Origin;     //车道当前车道，初始化时，车道的当前车道就是生成的原始车道
            this.LaneID_Previous = LaneID_Current;   //车道之前所在车道，暂时设为原车道


            this.LaneIDOrigin = int.Parse(LaneID_Origin[LaneID_Origin.Length - 2].ToString());      //之前所在车道 定义为1,2,3,4
            this.LaneID = int.Parse(LaneID_Origin[LaneID_Origin.Length - 2].ToString());            //当前所在车道 定义为1,2,3,4
            this.LaneIDFinal = int.Parse(LaneID_Final[LaneID_Final.Length - 2].ToString());       //最终到达车道


            //设置所属路口ID
            Lane lane = Lane.FindLaneByID(LaneID_Origin);      //获取当前车辆所在车道
            Intersection intersection = Intersection.FindIntersectionByID(lane.GetEnterCEIntID());                            
            string intersectionid = intersection.GetID();
            int intersectionnum = int.Parse(intersectionid.Substring(3, 2));
            this.IntersectionID = intersectionid;
            this.IntersectionNumber = intersectionnum;


            //设置车辆最终路口ID
            Lane finallane = Lane.FindLaneByID(LaneID_Final);
            Intersection finalintersection = Intersection.FindIntersectionByID(finallane.GetLeaveCEIntID());
            string intersectionfianlid = finalintersection.GetID();
            int intersectionfinalnum = int.Parse(intersectionfianlid.Substring(3, 2));
            this.IntersectionFinalID = intersectionfianlid;
            this.IntersectionFinalNumber = intersectionfinalnum;


            //规划设计路径           
            SetVehiclePath();     //设置规划好的VehiclePath，将Intersection写入


            //根据规划路径计算下一步车道
            if (IntersectionPath.Count == 1)  //在一个路口转向即可
            {
                this.LaneID_Next = this.LaneID_Final;
                this.LaneIDDestination = int.Parse(LaneID_Next[LaneID_Next.Length - 2].ToString());   //目标的下一个车道 定义为1,2,3,4
            }
            //最需规划
            else 
            {
                //设置下一个路口属性
                IntersectionNextID = IntersectionPath[1];
                IntersectionNextNumber = int.Parse(IntersectionNextID.Substring(3, 2));

                //根据当然车道，下一个路口，计算下一步车道
                this.LaneID_Next = MainData.FindLaneByInterID(this.IntersectionID, this.IntersectionNextID);
                this.LaneIDDestination = int.Parse(LaneID_Next[LaneID_Next.Length - 2].ToString());   //目标的下一个车道 定义为1,2,3,4               

            }


            //this.LaneID_Next = VehiclePath[1];       //暂时这么写？？？？？？以后设置下一步

            
            this.CarID = CarID;                      //车辆的ID
            this.CarType = CarType;                  //车辆的类型
            this.GenerationTime = GenerationTime;    //车辆的生成时间
            this.Sche_Priority_Origin = Priority;    //原来的优先级
            

            //如果是小车应该改变的属性值
            if (CarType == Definitions.SmallCar)
            {
                this.CarLENGTH = 3.6f;
                this.CarWIDTH = 1.5f;
            }
            //如果是大车应该改变的属性值
            else 
            {
                this.CarLENGTH = 4.5f;
                this.CarWIDTH = 1.6f;
            }
                                
            ////xc 设置车道
            ////为了保证兼容性需要对LaneIDOrigin、LaneIDDestion、LaneID设置
            //this.LaneID_Origin = VehiclePath[0];                            //车辆生成的原始车道
            //this.LaneID_Final = VehiclePath[VehiclePath.Length-1];          //车辆最终到达车道
            //this.LaneID_Current = LaneID_Origin;     //车道当前车道，初始化时，车道的当前车道就是生成的原始车道
            //this.LaneID_Previous = LaneID_Current;
            //this.LaneID_Next = VehiclePath[1];        //暂时这么写？？？？？？以后设置下一步

            
            //this.LaneID = int.Parse(LaneID_Origin[LaneID_Origin.Length - 2].ToString());
            //this.LaneIDDestination = int.Parse(LaneID_Final[LaneID_Final.Length - 2].ToString());
            //this.LaneIDOrigin = int.Parse(LaneID_Origin[LaneID_Origin.Length - 2].ToString());

            
            //this.LaneID_Origin_Map = LaneID_Origin_Map; 
            //this.LaneID_Final_Map = LaneID_Final_Map;
            

            this.Act_Direction_Origin = Directions.TurningDirection[this.LaneIDOrigin, this.LaneIDDestination];

            this.SCS_SectionID = SCS_ID.SCS_IDs[this.CarType, this.LaneID, this.LaneIDDestination]; ;     //将要占用的车道
            this.Act_Radius = Coordinate.Radius[this.CarType];  //车辆转弯半径，大车小车的转弯半径不同
            this.Act_Radius = Coordinate.Radius[this.CarType];  //车辆转弯半径
            this.Sche_Priority_Current = this.Sche_Priority_Origin;   //当前的优先级，可变动*/
            this.Act_Speed_Current = Vehicle.MAX_SPEED;    //车辆速度
            this.Act_Acceleration_Current = 0;  //加速度
            this.Act_Distance_Current = 0;      //车辆的5个点在一个时间片段中所走过的距离
            this.Act_Distance_Total = 0;        //车辆的5个点的所走的总路程

            this.State_Current = Definitions.Entering; //车辆所处状态
            this.Act_Direction_Current = this.Act_Direction_Origin;
            this.Act_Radian_Current = 0.0f;     //当前转角

            this.SCS_Count = this.SCS_SectionID.Length;
            this.HasToken = new bool[5] { false, false, false, false, false };   //标志车辆是否获得临界区
            

            this.Front_ID = null;   /*前车的ID（跟车时使用）*/
            this.FrontVehicle = null;       //前车
            this.FrontNewVehicle = null;    //换道之后的新的前车
            this.FrontVehicleChanged = false;   //标识是否出现新的前车

            this.State_AlreadyTS = new bool[5] { false, false, false, false, false };
            this.State_AlreadyTE = new bool[5] { false, false, false, false, false };
            this.State_AlreadyLeave = false;
            this.TExpectedTs = new float[5] { 0, 0, 0, 0, 0 };  //理想到达各临界区的时间，空间为5，顺序为1-4
            this.TRealTs = new float[5] { 0, 0, 0, 0, 0 };      //实际到达各临界区的时间，空间为5，顺序为1-4
            this.TExpectedTe = new float[5] { 0, 0, 0, 0, 0 };  //理想离开各临界区的时间，空间为5，顺序为1-4
            this.TRealTe = new float[5] { 0, 0, 0, 0, 0 };      //实际离开各临界区的时间，空间为5，顺序为1-4
            this.TIdealTs = new float[5] { 0, 0, 0, 0, 0 };     //车辆在无堵塞的情况下（最高速度）的情况下，到达各临界区的时间
            this.TIdealTe = new float[5] { 0, 0, 0, 0, 0 };     //车辆在无堵塞的情况下（最高速度）的情况下，离开各临界区的时间


            this.SpeedRecordQueue = new Dictionary<float, float[]>();        //每个TimeStep中记录当前车辆的速度，以备后续程序显示

            this.TimeInsert = 0.0f;         //车辆的插入时间，用以控制车辆的插入时间
            this.TimePreRecord = 0.0f;      //用以记录该车辆上一次动作的时刻，若现在 (总体时间 - 上次动作时间 >= 时间片段)，则可进行下一次动作

            this.State_InLane = false;      //标识车辆是否已经处于监控区域之中
            this.TimeEnter = 0;         //车辆进入监控区的时间
            this.TimeReservation = 0;   //车辆通过预约线的时间
            this.TimeDelete = 0;        //车辆删除时间

            this.State_DeadLineMark = 1;    //标志车辆现今处于第几个deadline之前
            this.State_Turn = Definitions.PreTurn;
            this.State_Arrive = Definitions.ArrivePreSections;
            this.State_Leave = Definitions.LeaveInit;
            this.sendMessageAllowed = false;

            this.Coor_X = new float[6] { 0, 0, 0, 0, 0, 0 }; //车辆的X坐标
            this.Coor_X[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].X;
            this.Coor_Y = new float[6] { 0, 0, 0, 0, 0, 0 }; //车辆的Y坐标     
            this.Coor_Y[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].Y;
            this.Coor_TurnPre_X = new float[6] { 0, 0, 0, 0, 0, 0 };    //车辆转弯前的X坐标
            this.Coor_TurnPre_Y = new float[6] { 0, 0, 0, 0, 0, 0 };    //车辆转弯前的Y坐标
            this.SetCoordinatesStraight();
            this.SetTurnPreXY();
            this.State_IsDisUpadated = false;
            this.Update_Act_Dis_Total_Correction();

            this.SCS_IDs_Leave = new Queue<int>();    //存储车辆需要使用的S-CS编号
            this.SCS_Array = new int[5];  //标志临界区申请的顺序
            this.SCS_Count = 0; //车辆需申请的临界区的数量

            string strtmp = SCS_ID.SCS_IDs_Leave[this.CarType, this.LaneIDOrigin, this.LaneIDDestination];
            for (int i = 0; i < this.SCS_SectionID.Length; i++)
            {
                this.SCS_IDs_Leave.Enqueue(int.Parse(strtmp[i].ToString()));
                this.HasToken[int.Parse(this.SCS_SectionID[i].ToString())] = false;
                this.SCS_Array[this.SCS_Count] = int.Parse(this.SCS_SectionID[i].ToString());
                this.SCS_Count++;
            }
                        
        }

        //设置车辆需要的车道、临界区信息
        private void SetLaneAndSection() 
        {
                                 

        }

        //按照当前所在车道，以及最终的目标车道，计算下一步的车道
        public void SetVehiclePath()
        {
            //获取节点路由表，找到合适的路由表（静态的）
            VehiclePathDijsktra_Sche = new VehiclePathDijsktra_Sche();
            VehiclePathDijsktra_Sche.VehiclePathPlan();
            string[,] VehiclePathDia = VehiclePathDijsktra_Sche.GetPathRouting();

            //获取节点路由表，找到合适的路由表（动态的）
            //string[,] VehiclePathDia = TrafficCloud.GetPathRouting();


            string path = "";

            //判断生成路口 和 终止路口
            if (this.IntersectionNumber == this.IntersectionFinalNumber)
            {
                path = "" + this.IntersectionNumber;
                PathNumber = path;
            }
            //需要规划
            else 
            {
                path = VehiclePathDia[this.IntersectionNumber - 1, this.IntersectionFinalNumber - 1];
                PathNumber = path;
            }                      

            //path路径解析成为
            for (int i = 0; i < path.Length; i++) 
            {
                string temp = path.Substring(i,1);
                if (temp != "-")
                {              
                    string pathintersection =  MainData.FindIntersectionByNum(int.Parse(temp)+1);
                    IntersectionPath.Add(pathintersection);      //规划出的路径使用Intersection表示
                }
            }
        }

        /************************************************************************/
        /* 设置车辆的转弯开始时的路径                                                                     */
        /************************************************************************/
        private void SetTurnPreXY()
        {
            this.Coor_TurnPre_X[Definitions.FrontMiddle] = Coordinate.TurnPreXY[this.CarType, this.LaneIDOrigin, this.Act_Direction_Origin].X;
            this.Coor_TurnPre_Y[Definitions.FrontMiddle] = Coordinate.TurnPreXY[this.CarType, this.LaneIDOrigin, this.Act_Direction_Origin].Y;

            switch (this.LaneIDOrigin)
            {
                case Definitions.WtoE_Lane:
                    this.Coor_TurnPre_X[Definitions.FrontLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle];
                    this.Coor_TurnPre_Y[Definitions.FrontLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_X[Definitions.FrontRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle];
                    this.Coor_TurnPre_Y[Definitions.FrontRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_X[Definitions.BackLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle] - this.CarLENGTH;
                    this.Coor_TurnPre_Y[Definitions.BackLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_X[Definitions.BackRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle] - this.CarLENGTH;
                    this.Coor_TurnPre_Y[Definitions.BackRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    break;
                case Definitions.EtoW_Lane:
                    this.Coor_TurnPre_X[Definitions.FrontLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle];
                    this.Coor_TurnPre_Y[Definitions.FrontLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_X[Definitions.FrontRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle];
                    this.Coor_TurnPre_Y[Definitions.FrontRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_X[Definitions.BackLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle] + this.CarLENGTH;
                    this.Coor_TurnPre_Y[Definitions.BackLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_X[Definitions.BackRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle] + this.CarLENGTH;
                    this.Coor_TurnPre_Y[Definitions.BackRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    break;
                case Definitions.NtoS_Lane:
                    this.Coor_TurnPre_X[Definitions.FrontLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.FrontLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle];
                    this.Coor_TurnPre_X[Definitions.FrontRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.FrontRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle];
                    this.Coor_TurnPre_X[Definitions.BackLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.BackLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] - this.CarLENGTH;
                    this.Coor_TurnPre_X[Definitions.BackRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.BackRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] - this.CarLENGTH;
                    break;
                case Definitions.StoN_Lane:
                    this.Coor_TurnPre_X[Definitions.FrontLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.FrontLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle];
                    this.Coor_TurnPre_X[Definitions.FrontRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.FrontRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle];
                    this.Coor_TurnPre_X[Definitions.BackLeft] = this.Coor_TurnPre_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.BackLeft] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] + this.CarLENGTH;
                    this.Coor_TurnPre_X[Definitions.BackRight] = this.Coor_TurnPre_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                    this.Coor_TurnPre_Y[Definitions.BackRight] = this.Coor_TurnPre_Y[Definitions.FrontMiddle] + this.CarLENGTH;
                    break;
            }

            this.Coor_TurnPre_X[Definitions.FrontMiddle] = (float)Math.Round(this.Coor_TurnPre_X[Definitions.FrontMiddle], Definitions.Precise);
            this.Coor_TurnPre_Y[Definitions.FrontMiddle] = (float)Math.Round(this.Coor_TurnPre_Y[Definitions.FrontMiddle], Definitions.Precise);
            this.Coor_TurnPre_X[Definitions.FrontLeft] = (float)Math.Round(this.Coor_TurnPre_X[Definitions.FrontLeft], Definitions.Precise);
            this.Coor_TurnPre_Y[Definitions.FrontLeft] = (float)Math.Round(this.Coor_TurnPre_Y[Definitions.FrontLeft], Definitions.Precise);
            this.Coor_TurnPre_X[Definitions.FrontRight] = (float)Math.Round(this.Coor_TurnPre_X[Definitions.FrontRight], Definitions.Precise);
            this.Coor_TurnPre_Y[Definitions.FrontRight] = (float)Math.Round(this.Coor_TurnPre_Y[Definitions.FrontRight], Definitions.Precise);
            this.Coor_TurnPre_X[Definitions.BackLeft] = (float)Math.Round(this.Coor_TurnPre_X[Definitions.BackLeft], Definitions.Precise);
            this.Coor_TurnPre_Y[Definitions.BackLeft] = (float)Math.Round(this.Coor_TurnPre_Y[Definitions.BackLeft], Definitions.Precise);
            this.Coor_TurnPre_X[Definitions.BackRight] = (float)Math.Round(this.Coor_TurnPre_X[Definitions.BackRight], Definitions.Precise);
            this.Coor_TurnPre_Y[Definitions.BackRight] = (float)Math.Round(this.Coor_TurnPre_Y[Definitions.BackRight], Definitions.Precise);
        }


        /************************************************************************/
        /**
         * 对外提供的车辆控制的接口，用于完成一个时间片之内的、在演示区内的车辆的运动控制
         */
        /************************************************************************/
        public void vehicleControl()
        {
            //当前时间减去上次记录的时间 大于一个时间片的间隔
            if ((float)Math.Round(Main.getGlobalTime() - this.TimePreRecord, Definitions.Precise) >= Definitions.RecordInterval)
            {
                this.recordCurrentSpeed(this.Act_Speed_Current);                                    //采用字典的数据类型记录当前速度
                this.TimePreRecord = (float)Math.Round(Main.getGlobalTime(), Definitions.Precise);  //更新上次速度记录时间
            }

            this.readMessageFromAgent();    //Agent发给车辆的信息解析
            this.vehicleBehavior();         //车辆运动控制

            //*****************
            //车辆的路口变换
            //*****************
            if (this.Get_X(1) >= 187 && this.LaneID == 1)
            {

                isVehicleEnd();
                //int ss = this.LaneID;
                //Console.Out.Write("LaneID " + ss);
                //Console.Out.Write("LaneID " + this.Get_Current_LaneID());
                //Console.Out.Write("x" + this.Get_X(1) + "y" + this.Get_Y(1));
            }

            if (this.Get_X(1) <= 0 && this.LaneID == 2)
            {

                isVehicleEnd();
                //int ss = this.LaneID;
                //Console.Out.Write("LaneID " + ss);
                //Console.Out.Write("LaneID " + this.Get_Current_LaneID());
                //Console.Out.Write("x" + this.Get_X(1) + "y" + this.Get_Y(1));
            }

            if (this.Get_Y(1) >= 187 && this.LaneID == 3)
            {
                isVehicleEnd();
                //int ss = this.LaneID;
                //Console.Out.Write("LaneID" + ss);
                //Console.Out.Write("LaneID " + this.Get_Current_LaneID());
                //Console.Out.Write("x" + this.Get_X(1) + "y" + this.Get_Y(1));
            }
            if (this.Get_Y(1) <= 0 && this.LaneID == 4)
            {
                isVehicleEnd();
                //int ss = this.LaneID;
                //Console.Out.Write("LaneID" + ss);
                //Console.Out.Write("LaneID " + this.Get_Current_LaneID());
                //Console.Out.Write("x" + this.Get_X(1) + "y" + this.Get_Y(1));
            }


            //按照周期发送消息
            if (this.PathPlaneTime == 0)
            {
                SendMessageToCloud();
            }

            //车辆请求的周期
            this.PathPlaneTime++;
            if (this.PathPlaneTime == 40) 
            {
                this.PathPlaneTime = 0;
            }


            //判断 动态路径规划 还是 静态路径规划
            if (MainData.FormConfig.GetDijkstraD())
            {
                //满足更新条件 更新车辆路径
                if (CanUpdatePath())
                {
                    UpdateVehiclePath();
                }
                else
                {
                    //将消息清除
                    int index = -1;
                    List<C2VMessage> C2VMessageList = MainData.Map.C2VBuffer;
                    for (int i = 0; i < C2VMessageList.Count; i++)
                    {
                        if (C2VMessageList[i].GetReceiver().Equals(this.CarID))
                        {
                            index = i;
                        }
                    }
                    //去除消息
                    if (index >= 0)
                    {
                        MainData.Map.C2VBuffer.RemoveAt(index);
                    }
                }

            }
            else 
            {
                //静态路径规划 啥也不做就行了
            }

           


        }

        //车辆给交通云发消息
        public void SendMessageToCloud() 
        {
            V2CMessage V2CMessage = new V2CMessage();
            V2CMessage.SetSender(this.CarID);
            V2CMessage.SetReceiver(MainData.Map.GetID());
            V2CMessage.SetType(Definitions.MsgApply);
            MainData.Map.V2CBuffer.Add(V2CMessage);

        }

        //更新车辆的最新最快路径
        public void UpdateVehiclePath() 
        {
            int flag =0;  //更新路径 标志位
            int index = 0;
            int Row = MainData.CEIntersectionList.Count;
            string[,] VehiclePathDia = new string[Row, Row];   
            List<C2VMessage> C2VMessageList = MainData.Map.C2VBuffer;
            for(int i=0;i<C2VMessageList.Count;i++)
            {
                if (C2VMessageList[i].GetReceiver().Equals(this.CarID)) 
                {
                    VehiclePathDia = C2VMessageList[i].GetPathRoute();
                    flag = 1;
                    index = i;
                }
            }

            //可以更新
            if (flag == 1) 
            {
                //去除消息
                MainData.Map.C2VBuffer.RemoveAt(index);

                //车辆当前所在车道可以到达路口，即能规划的下一个路口
                Intersection PlanIntersection = null;
                for (int i = 0; i < MainData.CEIntersectionList.Count(); i++)
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    List<Lane> LaneList = Intersection.GetEnterIntLaneList();
                    for (int j = 0; j < LaneList.Count(); j++)
                    {
                        Lane Lane = LaneList[j];
                        if (Lane.GetID().Equals(this.LaneID_Current))
                        {
                            PlanIntersection = Intersection; //可以规划的路口
                        }
                    }
                }


                //可以规划的路口ID、number
                string PlaneIntersectionID = PlanIntersection.GetID();
                int PlaneIntersectionNum = int.Parse(PlaneIntersectionID.Substring(3, 2));


                //根据可以规划路口、之后的路径删掉   
                List<string> IntersectionList = this.IntersectionPath;
                int Flag = 0;
                int Count = IntersectionList.Count;
                for (int i = 0; i < IntersectionList.Count; i++)
                {
                    if (IntersectionList[i].Equals(PlaneIntersectionID))
                    {
                        Flag = i;
                    }
                }

                //将后面的路径移除
                for (int j = Count - 1; j >= Flag; j--)
                {
                    this.IntersectionPath.RemoveAt(j);
                }
                PathNumber = PathNumber.Substring(0, Flag);



                //获取最新的路径规划路由表，找到合适的路由表（静态的）

                string path = "";
                path = VehiclePathDia[PlaneIntersectionNum - 1, this.IntersectionFinalNumber - 1];
                if (Flag > 0)
                {
                    PathNumber += "-" + path;
                }
                else
                {
                    PathNumber += path;
                }


                //path路径解析成为
                for (int i = 0; i < path.Length; i++)
                {
                    string temp = path.Substring(i, 1);
                    if (temp != "-")
                    {
                        string pathintersection = MainData.FindIntersectionByNum(int.Parse(temp) + 1);
                        IntersectionPath.Add(pathintersection);      //规划出的路径使用Intersection表示
                    }
                }


                //更新NextLane NextIntersection
                if (PlaneIntersectionID.Equals(this.IntersectionID))
                {
                    this.IntersectionNextID = this.IntersectionPath[Flag + 1];
                    this.IntersectionNextNumber = int.Parse(IntersectionNextID.Substring(3, 2));


                    this.LaneID_Next = MainData.FindLaneByInterID(this.IntersectionID, this.IntersectionNextID);
                    this.LaneIDDestination = int.Parse(LaneID_Next[LaneID_Next.Length - 2].ToString());   //目标的下一个车道 定义为1,2,3,4               

                    this.Act_Direction_Origin = Directions.TurningDirection[this.LaneIDOrigin, this.LaneIDDestination];
                    this.SCS_SectionID = SCS_ID.SCS_IDs[this.CarType, this.LaneID, this.LaneIDDestination]; ;     //将要占用的车道

                    this.SCS_IDs_Leave = new Queue<int>();    //存储车辆需要使用的S-CS编号
                    this.SCS_Array = new int[5];  //标志临界区申请的顺序
                    this.SCS_Count = 0; //车辆需申请的临界区的数量

                    string strtmp = SCS_ID.SCS_IDs_Leave[this.CarType, this.LaneIDOrigin, this.LaneIDDestination];
                    for (int i = 0; i < this.SCS_SectionID.Length; i++)
                    {
                        this.SCS_IDs_Leave.Enqueue(int.Parse(strtmp[i].ToString()));
                        this.HasToken[int.Parse(this.SCS_SectionID[i].ToString())] = false;
                        this.SCS_Array[this.SCS_Count] = int.Parse(this.SCS_SectionID[i].ToString());
                        this.SCS_Count++;
                    }
                    this.SetTurnPreXY();
                }            

            }
            
        }

        //判断车辆是否可以更新Path 返回true表示可以更新 返回false表示不能更新
        public bool CanUpdatePath() 
        {
            //所在路口是否为终止路口
            bool EndIntersectionFlag = true;
            if(this.IntersectionNumber == this.IntersectionFinalNumber) 
            {
                EndIntersectionFlag = false; 
            }

            //所处位置是否在预约线之外
            bool ArriveReservationLineFlag = true;
            if (this.TimeReservation > 0) 
            {
                ArriveReservationLineFlag = false; //不能
            }

            //判断所在车道是进入终止路口的车道
            bool EndLaneFlag = true;
            List<Lane> LaneList = MainData.FindIntersectionByID(this.IntersectionID).GetEnterIntLaneList();
            for (int i = 0; i < LaneList.Count(); i++) 
            {
                if (LaneList[i].GetID().Equals(this.LaneID)) 
                {
                    EndLaneFlag = false;
                }
            }
            

            //判断是否可以更新路径
            if (EndIntersectionFlag && ArriveReservationLineFlag && EndLaneFlag)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /************************************************************************/
        /* 记录当前time下的车辆的速度                                                                     */
        /************************************************************************/
        private void recordCurrentSpeed(float Speed)
        {
            //字典类型： float, float[]
            //float key 当前总行驶距离
            //flaot[] [0]当前时间 [1]当前速度
            float[] record = new float[3];

            //根据字典的Key判断是否已经记录过该速度
            if (!this.SpeedRecordQueue.Keys.Contains((float)Math.Round(this.Act_Distance_Total, Definitions.Precise)))
            {
                record[0] = (float)(Math.Round(Main.getGlobalTime(), Definitions.Precise));
                record[1] = (float)Math.Round(this.Act_Speed_Current, Definitions.Precise);
                this.SpeedRecordQueue.Add((float)Math.Round(this.Act_Distance_Total, Definitions.Precise), record);
            }

        }


        /************************************************************************/
        /* 读取从Agent反馈给汽车的消息，
         * 将信息缓冲区中的信息，属于该车辆的信息存入到该车辆的AgentToVehicle队列中
         */
        /************************************************************************/
        //private void readMessageFromAgent()
        //{
        //    Bean.Message mtmp = null;

        //    /*从缓冲区中读取Agent发送给车辆的消息*/
        //    //获取当前车辆所在车道
        //    Intersection intersection = Intersection.FindIntersectionByID(this.IntersectionID);   //获取当前车辆进入的路口
        //    List<Bean.Message> AtoV_Buffer = intersection.GetAtoV_Buffer();       //拿到车辆所在路口的所有消息            


        //    //消息队列中存放的消息
        //    if (AtoV_Buffer.Count > 0)
        //    {

        //        List<Bean.Message> AtoV_BufferCopy = new List<Bean.Message>();            //复制AtoV_Buffer，留作临时变量

        //        List<Bean.Message> MessagesFromAgent = new List<Bean.Message>();      //存储从Agent到该ID车辆的消息队列

        //        foreach (Bean.Message m in AtoV_Buffer)
        //        {
        //            AtoV_BufferCopy.Add(m);
        //        }


        //        //处理这些消息，并在处理完成后，写回去            
        //        for (int i = 0; i < AtoV_Buffer.Count; i++)
        //        {
        //            Bean.Message m = AtoV_Buffer[i];
        //            if (m.GetReceiver() == this.CarID)                              //遍历将该路口发给此车辆的Message      
        //            {
        //                MessagesFromAgent.Add(m);                                   //存放到该车辆的Message到MessagesFromAgent队列中
        //                AtoV_BufferCopy.Remove(m);
        //            }
        //        }


        //        intersection.SetAtoV_Buffer(AtoV_BufferCopy);
        //        MainData.UpdateIntersectionList(intersection);    //将数据写回去

        //        /*处理接收到的消息*/
        //        while (MessagesFromAgent.Count != 0)
        //        {
        //            mtmp = MessagesFromAgent.ElementAt(0);
        //            MessagesFromAgent.Remove(mtmp);

        //            //授权信息
        //            if (mtmp.GetType() == Definitions.MsgAuthorize)
        //            {
        //                if (this.HasToken[int.Parse(mtmp.GetSectionID())] == false)
        //                {
        //                    this.HasToken[int.Parse(mtmp.GetSectionID())] = true;
        //                    this.State_DeadLineMark++;
        //                }
        //                this.IsAuth = true;
        //            }

        //            //拒绝信息
        //            else if (mtmp.GetType() == Definitions.MsgRefuse)
        //            {

        //            }
        //            //其他信息     
        //            else
        //            {
        //                /*目前为止Agent只会对车辆发送授权或拒绝消息*/
        //                System.Console.WriteLine("Wrong type of message from Agent ");
        //                return;
        //            }
        //        }
        //    }
        //}

        private void readMessageFromAgent()
        {
            Bean.Message mtmp = null;

            /*从缓冲区中读取Agent发送给车辆的消息*/
            //获取当前车辆所在车道
            Intersection intersection = Intersection.FindIntersectionByID(this.IntersectionID);   //获取当前车辆进入的路口
            List<Bean.Message> AtoV_Buffer = intersection.GetAtoV_Buffer();       //拿到车辆所在路口的所有消息            


            //消息队列中存放的消息
            if (AtoV_Buffer.Count > 0)
            {              

                List<Bean.Message> MessagesFromAgent = new List<Bean.Message>();      //存储从Agent到该ID车辆的消息队列
                MessagesFromAgent.Clear();

                //处理这些消息，并在处理完成后，写回去            
                for (int i = 0; i < AtoV_Buffer.Count; i++)
                {                 
                    
                    Bean.Message m = AtoV_Buffer[i];

                    ////Test将授权信息全部写入到文件中 2018.5.28
                    ////TsetRecordMessageInformation(m);
                    //DateTime DT = System.DateTime.Now;
                    //string text = System.IO.File.ReadAllText(@"D:\IOTest\IOTestAcceptAllMessage.txt");
                    //text += DT.ToString()+" "+this.CarID + " "+intersection.GetID() + " " + m.GetLaneID() + " " + m.GetReceiver() + " " + m.GetSectionID() + "\r\n";
                    //System.IO.File.WriteAllText(@"D:\IOTest\IOTestAcceptAllMessage.txt", text);
                    ////Test

                    if (m.GetReceiver().Equals(this.CarID))                              //遍历将该路口发给此车辆的Message      
                    {
                        MessagesFromAgent.Add(m);                                   //存放到该车辆的Message到MessagesFromAgent队列中

                        //Test将授权信息全部写入到文件中 2018.5.28
                        //TsetRecordMessageInformation(m);
                        //string text1 = System.IO.File.ReadAllText(@"D:\IOTest\IOTestID.txt");
                        //text1 += this.CarID + " " + intersection.GetID() + " " + m.GetLaneID() + " " + m.GetReceiver() + " " + m.GetSectionID() + "\r\n";
                        //System.IO.File.WriteAllText(@"D:\IOTest\IOTestID.txt", text1);
                        //Test

                    }
                }

                //起初要处理的消息
                for (int i = 0; i < MessagesFromAgent.Count; i++)
                {
                    AtoV_Buffer.Remove(MessagesFromAgent[i]);

                    Bean.Message m = MessagesFromAgent[i];
                    //Test将授权信息全部写入到文件中 2018.5.28
                    //TsetRecordMessageInformation(m);
                    //string text1 = System.IO.File.ReadAllText(@"D:\IOTest\IORemove.txt");
                    //text1 += this.CarID + " " + intersection.GetID() + " " + m.GetLaneID() + " " + m.GetReceiver() + " " + m.GetSectionID() + "\r\n";
                    //System.IO.File.WriteAllText(@"D:\IOTest\IORemove.txt", text1);
                }


                intersection.SetAtoV_Buffer(AtoV_Buffer);
                MainData.UpdateIntersectionList(intersection);    //将数据写回去
                              

                /*处理接收到的消息*/
                while (MessagesFromAgent.Count != 0)
                {
                    mtmp = MessagesFromAgent.ElementAt(0);
                    MessagesFromAgent.Remove(mtmp);


                    //Test将授权信息全部写入到文件中 2018.5.28
                    //TsetRecordMessageInformation(m);
                    //string text = System.IO.File.ReadAllText(@"D:\IOTest\IOTestV.txt");
                    //text += intersection.GetID() + " " + mtmp.GetLaneID() + " " + mtmp.GetReceiver() + " " + mtmp.GetSectionID() + "\r\n";
                    //System.IO.File.WriteAllText(@"D:\IOTest\IOTestV.txt", text);


                    //授权信息
                    if (mtmp.GetType() == Definitions.MsgAuthorize)
                    {
                        if (this.HasToken[int.Parse(mtmp.GetSectionID())] == false)
                        {
                            this.HasToken[int.Parse(mtmp.GetSectionID())] = true;
                            this.State_DeadLineMark++;
                        }
                        this.IsAuth = true;
                    }

                    //拒绝信息
                    else if (mtmp.GetType() == Definitions.MsgRefuse)
                    {

                    }

                     //xc 为了提升优先级，刚刚添加
                    else if (mtmp.GetType() == Definitions.MsgRaisePrio)   //车辆提升优先级
                    {
                        if (mtmp.GetPriority() != this.Sche_Priority_Current)
                        {
                            float[] record = new float[3];
                            record[0] = this.Act_Distance_Total;
                            record[1] = this.Sche_Priority_Current;
                            record[2] = mtmp.GetPriority();
                            this.Raise_Priority.Add(Main.getGlobalTime(), record);

                            /*消息中包含在调度器处理过程中该车优先级的变动*/
                            this.Sche_Priority_Current = mtmp.GetPriority();
                            this.Sche_Priority_Changed = mtmp.GetPriority();
                        }
                    }    

                    //其他信息     
                    else
                    {
                        /*目前为止Agent只会对车辆发送授权或拒绝消息*/
                        System.Console.WriteLine("Wrong type of message from Agent ");
                        return;
                    }
                }
            }
        }

        ///************************************************************************/
        ///* 读取从Agent反馈给汽车的消息，
        // * 将信息缓冲区中的信息，属于该车辆的信息存入到车辆的AgentToVehicle队列中
        // */
        ///************************************************************************/
        //private void readMessageFromAgent()
        //{
        //    Message mtmp;
        //    /*从缓冲区中读取Agent发送给车辆的消息*/
        //    foreach (Message m in Msg_Buffer.AtoV_Buffer)
        //    {
        //        if (m.getReceiver() == this.CarID)
        //        {
        //            this.MessagesFromAgent.Enqueue(m);
        //        }
        //    }
        //    /*处理接收到的消息*/
        //    while (this.MessagesFromAgent.Count != 0)
        //    {
        //        mtmp = this.MessagesFromAgent.Dequeue();
        //        if (mtmp.getType() == Definitions.Msg_Authorize)   //若mtmp的信息类型为授权信息
        //        {
        //            if (this.HasToken[int.Parse(mtmp.getSectionID())] == false)
        //            {
        //                this.HasToken[int.Parse(mtmp.getSectionID())] = true;
        //                this.State_DeadLineMark++; //标志车辆现今处于哪一个deadline之前 ？？？
        //            }
        //            if (mtmp.getPriority() != this.Sche_Priority_Current)  //Current是原始优先级，getPriority是现在的优先级？？？
        //            {
        //                float[] record = new float[3];
        //                record[0] = this.Act_Distance_Total;
        //                record[1] = this.Sche_Priority_Current;
        //                record[2] = mtmp.getPriority();
        //                this.Raise_Priority.Add(Main.getGlobalTime(), record);//其中元素分别是车辆提升优先级的时间、位置、
        //                //车辆提升前优先级、车辆提升后优先级

        //                /*消息中包含在调度器处理过程中该车优先级的变动*/
        //                this.Sche_Priority_Current = mtmp.getPriority();
        //                this.Sche_Priority_Changed = mtmp.getPriority();  //int类型             
        //            }
        //            this.IsAuth = true;  //标志当前车辆是否已经被授权
        //        }
        //        else if (mtmp.getType() == Definitions.Msg_Refuse)    //拒绝授权信息
        //        {
        //            //                    this.HasToken[int.Parse(mtmp.getSectionID())] = false;
        //        }
        //        else if (mtmp.getType() == Definitions.Msg_RaisePrio)   //车辆提升优先级
        //        {
        //            if (mtmp.getPriority() != this.Sche_Priority_Current)
        //            {
        //                float[] record = new float[3];
        //                record[0] = this.Act_Distance_Total;
        //                record[1] = this.Sche_Priority_Current;
        //                record[2] = mtmp.getPriority();
        //                this.Raise_Priority.Add(Main.getGlobalTime(), record);

        //                /*消息中包含在调度器处理过程中该车优先级的变动*/
        //                this.Sche_Priority_Current = mtmp.getPriority();
        //                this.Sche_Priority_Changed = mtmp.getPriority();
        //            }
        //        }
        //        else
        //        {
        //            /*目前为止Agent只会对车辆发送授权或拒绝消息，和提升车辆优先级信息*/
        //            MessageBox.Show("Wrong type of message from Agent ---- " + this.CarID, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //            return;
        //        }
        //    }
        //}


        /************************************************************************/
        /* 车辆行为控制函数，根据当前车辆状态进行切换                                                                     */
        /************************************************************************/
        private void vehicleBehavior()
        {
            switch (this.State_Current)
            {
                case Definitions.Init:            //初始状态
                    break;
                case Definitions.Entering:        //进入状态
                    this.stateEnteringFunc();
                    break;
                case Definitions.Following:       //跟车状态                    
                    this.stateFollowingFunc();                   
                    break;
                case Definitions.Changing:        //变道状态
                    break;
                case Definitions.Overtaking:      //超车状态
                    break;
                case Definitions.Passing_Through: //转弯状态
                    this.statePassingFunc();                    
                    break;
                case Definitions.Parking:         //停车状态
                    //this.stateparkingFunc();
                    break;
                case Definitions.Terminate:       //终止状态
                    break;
                case Definitions.Waiting:         //等待状态
                    this.stateWaitingFunc();
                    break;
                case Definitions.Passed:          //通过状态
                    this.statePassedFunc();
                    break;
                default:
                    break;
            }
        }


        /************************************************************************/
        /**
          * 车辆在越过EnterLine之前的状态，可以跟车行驶，最高速度MAX_SPEED，
          * 下一个状态为Following状态
        */
        /************************************************************************/
        private void stateEnteringFunc()
        {
            //更新前车 （type进入前车）
            this.updateFrontVehicle(1);

            //如果车辆行驶的距离到达进入线
            if (this.Act_Distance_Total >= Coordinate.EnterLine)
            {
                if (!this.State_InLane)
                {
                    this.TimeEnter = Main.getGlobalTime() - Main.TimeStep / 2.0f;
                }
                this.State_InLane = true;

                //车辆进入预约区域，开始预约、进入Following状态
                if (this.Act_Distance_Total >= Coordinate.ReservationLine)
                {
                    //设置车辆所在路口  xc在预约线设置所在路口
                    //另外，可以在进入车道上时设定
                    Lane lane = Lane.FindLaneByID(this.Get_Current_LaneID());      //获取当前车辆所在车道
                    Intersection intersection = Intersection.FindIntersectionByID(lane.GetEnterCEIntID());
                    this.IntersectionID = intersection.GetID();                   
                    this.IntersectionNumber = int.Parse(this.IntersectionID.Substring(3, 2));
                                                          

                    //修改车辆状态  记录车辆进入预约线时间
                    if (this.State_Current == Definitions.Entering)
                        this.TimeReservation = Main.getGlobalTime() - Main.TimeStep / 2.0f;
                    this.State_Current = Definitions.Following;
                    this.drivingFunc(Vehicle.CRUISE_SPEED);  //按照巡航速度行驶
                    this.UpdateAuthrizeTime();               //更新车辆预约时间 


                    //将该车辆放入该路口的ReservationLst xc修齐治平 2018.5.8 
                    List<string> ListTemp = intersection.GetReservationLst();
                    ListTemp.Add(this.CarID);
                    intersection.SetReservationLst(ListTemp);
                    MainData.UpdateIntersectionList(intersection);


                    //将当前所在车道转化为LaneID，1、2、3、4
                    string LaneID_Temp = this.Get_Current_LaneID();
                    int LaneID = int.Parse(LaneID_Temp.Substring(0, 1));
                    //发送预约消息
                    this.sendMessage(Definitions.MsgReservation, this.CarID, Definitions.Agent, this.TExpectedTs, this.TExpectedTe, this.Sche_Priority_Current, this.SCS_SectionID, LaneID);

                                        
                    this.sendMessageAllowed = true;
                    return;
                }
                this.drivingFunc(Vehicle.CRUISE_SPEED);      //按照巡航速度行驶
            }

            //车辆行驶的距离没有达到进入线，按照最大速度行驶
            else
            {
                this.drivingFunc(Vehicle.MAX_SPEED);        //按照最大速度行驶
            }
        }


        /************************************************************************/
        /* 更新车辆的FrontVehicle，只需更新正在转弯中的车辆与刚插入的车辆 
         * type=0 车辆第一次插入车道
         * type=1 车辆进入车道
         * type=2 车辆进入临界区
        /************************************************************************/
        public void updateFrontVehicle(int type)
        {
            Vehicle pre = null;
            //加入下一个路口
            if (type == 4) 
            {
                Lane LaneCurTemp = Lane.FindLaneByID(this.LaneID_Current);
                List<Vehicle> VehicleListTemp = LaneCurTemp.GetVehicleList();

                for (int i = 0; i < VehicleListTemp.Count(); i++)
                {
                    Vehicle v = VehicleListTemp[i];
                    //if (v.Get_Current_LaneID() != this.LaneID_Current
                    //     || v.State_Leave == Definitions.LeavePreSections || v.State_Leave == Definitions.LeaveSections
                    //     || v.State_Leave == Definitions.LeaveAfterSections || v.Get_ID() == this.CarID)
                    if (v.Get_Current_LaneID() != this.LaneID_Current
                        || v.State_Leave == Definitions.LeavePreSections 
                        || v.State_Leave == Definitions.LeaveAfterSections 
                        )
                    {
                        //插入车辆时，所有的车辆已经更新过，即为下一时间片的情况，即当前被插入的车辆是在下一时间片开始时其total_dis为0，
                        continue;
                    }
                    if (v.Get_ID() == this.CarID) 
                    {
                        break;
                    }
                    pre = v;
                }

                //foreach (Vehicle v in LaneCurTemp.GetVehicleList())
                //{
                //    //if (v.Get_Current_LaneID() != this.LaneID_Current
                //    //     || v.State_Leave == Definitions.LeavePreSections || v.State_Leave == Definitions.LeaveSections
                //    //     || v.State_Leave == Definitions.LeaveAfterSections || v.Get_ID() == this.CarID)
                //    //{
                //    //    //插入车辆时，所有的车辆已经更新过，即为下一时间片的情况，即当前被插入的车辆是在下一时间片开始时其total_dis为0，
                //    //    continue;
                //    //}
                //    pre = v;
                //}

            }else if (type == 0)      //车辆第一次插入该车道
            {
                //foreach (Vehicle v in MainData.Map.GlobalVehicleList)
                Lane LaneCurTemp = Lane.FindLaneByID(this.LaneID_Current);
                foreach (Vehicle v in LaneCurTemp.GetVehicleList())
                {
                    if (v.Get_Current_LaneID() != this.LaneID_Current
                         || v.State_Leave == Definitions.LeavePreSections || v.State_Leave == Definitions.LeaveSections
                         || v.State_Leave == Definitions.LeaveAfterSections || v.Get_ID() == this.CarID)
                    {
                        //插入车辆时，所有的车辆已经更新过，即为下一时间片的情况，即当前被插入的车辆是在下一时间片开始时其total_dis为0，
                        continue;
                    }
                    pre = v;
                }
            }
            else if (type == 1)  //车辆已经进入车道了
            {
                //车辆已插入初始车道，判断该车辆的前车是否已经离开初始车道，进入临界区,若是，则删除之
                //因为该车辆的前车必然已经被更新过，所以此时判断的前车是当前时间片结束时的情况
                //此时则说明在下一个时间片中必然不会撞上前车，所以无需再考虑前车的问题
                pre = this.FrontVehicle;
                if (pre != null)
                {
                    if (pre.State_Leave != Definitions.LeaveInit)
                    {
                        this.FrontVehicle = null;                //该车辆的前车离开当前车道，那么将该车前车定位空                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                        pre = null;
                    }
                }
            }
            else if (type == 2)   //车辆进入临界区
            {
                //车辆已经进入临界区，且获得若干临界区，需按照是否获得所有临界区分别讨论前车的更新情况
                //无论前车是否已经更新，后车都有足够的距离以及时间来调整
                //1、若当前已获得最后的临界区，那么前车必然已经离开临界区，此时必然可以调整前车
                //2、若当前车未获得最后的临界区，那么没必要更新前车
                            

                int CSID = this.SCS_Array[this.SCS_Count - 1];
                
                if (this.HasToken[CSID] == true)           //所需的最后一个临界区，是否已经获得，如果获得那么说明所得有临界区都获得了
                {
                    //车辆已经获得所有临界区，此时需要更新前车
                    Lane lane = Lane.FindLaneByID(this.LaneID_Next); //获取将要到达的车道
                    //在已通过的目标车道中寻找车辆，直到找到当前车的前一辆，
                    //在当前车未进入目标车道的情况下，则找到目标车道的最后一辆车

                    List<Vehicle> VehicleListTemp = lane.GetVehicleList();
                    for (int i = 0; i < VehicleListTemp.Count(); i++)
                    {
                        Vehicle v = VehicleListTemp[i];
                        if (v.Get_ID().Equals(this.CarID) == false)
                        {
                            pre = v;                //前车
                        }
                        else
                        {
                            break;
                        }
                    }                                                       

                }
                else
                {
                    //未获得所有临界区，那么还没离开临界区，认为其没有前车
                    pre = null;
                }
            }


            //更新前车
            if (pre != null)    //如果该车辆存在前车
            {
                this.FrontVehicle = pre;
                this.Front_ID = pre.Get_ID();
                this.FrontVehicleChanged = true;
            }
            else               //如果该车辆不存在前车
            {
                this.FrontVehicle = null;
                this.Front_ID = null;
                this.FrontVehicleChanged = false;
            }


            //如果有前车，但是前车距离此车超过车道长度，那么认为没有前车
            //xc还需要修改
            //if (this.FrontVehicle != null && this.FrontVehicle.Get_Total_Distance() >= 190.0f)
            //{
            //    this.FrontVehicle = null;
            //    this.Front_ID = null;
            //}

        }

        /************************************************************************/
        /* 1、跟车函数，用于后车更前车，实现后车的不超线，不撞车，不超速
         * 2、更新当前时间片下的车辆的状态
         * 3.更新车辆的坐标
         */
        /************************************************************************/
        private void drivingFunc(float AllowedMaxSpeed)
        {
            //具体的前车的判断已由UpdateFrontVehicle实现，即此处建立在前车情况已知的前提下
            if (this.FrontVehicle != null)  //有前车，需检验是否会撞车
            {
                //验证是否能够加速
                if (this.TestAccelaration(AllowedMaxSpeed, true))
                {//测试是否能够加速
                    if (Math.Abs(this.Act_Acceleration_Current - Vehicle.ACCELERATION) > 0.1)
                    {
                        this.TestAccelaration(AllowedMaxSpeed, true);      //用于测试匀速检验函数的正确性
                    }
                    this.setAcceleration(Vehicle.ACCELERATION);
                }
                else if (this.TestConstantSpeed(AllowedMaxSpeed, true))
                {//测试是否能够匀速前进
                    if (Math.Abs(this.Act_Acceleration_Current) > 0.1)
                    {
                        this.TestConstantSpeed(AllowedMaxSpeed, true);      //用于测试匀速检验函数的正确性
                    }
                    this.setAcceleration(0);
                }
                else
                {
                    //if (Math.Abs(this.Act_Acceleration_Current - Vehicle.DECCELERATION) > 0.1)
                    //{
                    //    this.TestDecelation(AllowedMaxSpeed, true);
                    //}
                    this.setAcceleration(Vehicle.DECCELERATION);
                }
            }

            else
            {

                //验证是否能够加速
                if (this.TestAccelaration(AllowedMaxSpeed, false))     //测试是否能够加速
                {
                    if (Math.Abs(this.Act_Acceleration_Current - Vehicle.ACCELERATION) > 0.1)
                    {
                        this.TestAccelaration(AllowedMaxSpeed, false);      //用于测试匀速检验函数的正确性
                    }
                    this.setAcceleration(Vehicle.ACCELERATION);
                }
                else if (this.TestConstantSpeed(AllowedMaxSpeed, false)) //测试是否能够匀速前进  
                {
                    if (Math.Abs(this.Act_Acceleration_Current) > 0.1)
                    {
                        this.TestConstantSpeed(AllowedMaxSpeed, false);      //用于测试匀速检验函数的正确性
                    }
                    this.setAcceleration(0);
                }
                else
                {
                    //if (Math.Abs(this.Act_Acceleration_Current - Vehicle.DECCELERATION) > 0.1)
                    //{
                    //    this.TestDecelation(AllowedMaxSpeed, false);
                    //}
                    this.setAcceleration(Vehicle.DECCELERATION);
                    
                }
            }
   
            this.computeDrivingDistance(AllowedMaxSpeed);
            this.coordinateUpdate();
               
            if (this.sendMessageAllowed == true && this.Get_Current_Acceleration() != 0)
            {
                this.UpdateAuthrizeTime();

                //将当前所在车道转化为LaneID，1、2、3、4
                string LaneID_Temp = this.Get_Current_LaneID();
                int LaneID = int.Parse(LaneID_Temp.Substring(0, 1));
                //发送预约消息
                this.sendMessage(Definitions.MsgRenew, this.CarID, Definitions.Agent, this.TExpectedTs, this.TExpectedTe, this.Sche_Priority_Current, this.SCS_SectionID, LaneID);

            }          
            

        }


        /************************************************************************/
        /* 在加速条件下车辆将行驶的距离，此处保证了不超速，不过线，不撞车，以供验证是否能够加速    
         * AllowedMaxSpeed最大允许速度
         * 
        /************************************************************************/
        private bool TestAccelaration(float AllowedMaxSpeed, bool HaveFrontVehicle)
        {

            //当前车辆的下一个预约线
            //xc 需要修改每一条道路的deadline 
            /*
             * ????????????????????????????????
             * 需要修改
             *????????????????????????????????? 
             * */
            float deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, this.State_DeadLineMark];


            //规则一：不超速
            bool Enable1 = false;   //标识会否超速，若为true，则不会超线，可加速
            if (this.Act_Speed_Current < AllowedMaxSpeed)
            {
                Enable1 = true;
            }
            else
            {
                Enable1 = false;
            }

            float t = 0;     //可以加速的时间
            if ((this.Act_Speed_Current + Vehicle.ACCELERATION * Main.TimeStep) > AllowedMaxSpeed)
            {
                t = (AllowedMaxSpeed - this.Act_Speed_Current) / Vehicle.ACCELERATION;
            }
            else
            {
                t = Main.TimeStep;
            }
            if (t < 0)
            {
                t = 0;
            }


            //规则二：不超线
            bool Enable2 = false;   //标识会否超线，若为true，则不会超线，可加速
            //加速条件下，当前时间片结束时，车辆的速度
            float speed_next = this.Act_Speed_Current + Vehicle.ACCELERATION * t;
            //加速条件下，当前时间片所走过的距离
            float cur_dis = this.Act_Speed_Current * t + 0.5f * Vehicle.ACCELERATION * t * t + AllowedMaxSpeed * (Main.TimeStep - t);
            //加速条件下，当前时间片完成后，最小减速距离
            float next_dis = speed_next * speed_next / (-2.0f * Vehicle.DECCELERATION);
            if ((deadline - this.Act_Distance_Total - cur_dis - next_dis - Vehicle.SAFE_DISTANCE) > 0.00000001)    //  因为在一个时间片中，加速度固定，所以当加速时，一个时间片后的速度固定，即只需剩下的距离满足减速需求即可
            {//如果在临界区之前能够刹车完成，则加速至最大速度
                Enable2 = true;
            }
            else
            {
                Enable2 = false;
            }


            //规则三：不撞车
            bool Enable3 = false;   //标识会否撞车，若为true，则不会撞车，可加速
            if (HaveFrontVehicle)   //有前车
            {
                Vehicle pre = this.Get_FrontVehicle();
                
                //同一路口
                if (pre.GetIntersectionID().Equals(this.IntersectionID))
                {
                    if (this.State_Arrive == Definitions.ArrivePreSections)
                    {
                        //车辆未进入临界区，此时无需校准total_dis，无需使用Total_Correction_Dis                   
                      
                        float pre_speed = pre.Get_Current_Speed();
                        float Pre_next_length = (float)(pre_speed * pre_speed / 2.0 / MainData.SimulationConfig.GetTxtMaximumAcceleration());


                        //dis = 前车已行驶总路程 + 前车最小减速距离 – 前车车长 - 前车安全距离 – 后车行驶距离 - 后车最小减速距离 – 后车已行驶总路程 
                        float dis = pre.Get_Total_Distance() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                                    - cur_dis - next_dis - this.Act_Distance_Total;


                        if (dis >= 0)
                        {
                            Enable3 = true;
                        }
                        else
                        {
                            Enable3 = false;
                        }
                    }
                    else
                    {
                        //车辆已进入临界区，此时需要校准total_dis，需使用Total_Correction_Dis
                        
                        float pre_speed = pre.Get_Current_Speed();
                        float Pre_next_length = (float)(pre_speed * pre_speed / 2.0 / MainData.SimulationConfig.GetTxtMaximumAcceleration());

                        //dis = 前车已行驶总路程 + 前车最小减速距离 – 前车车长 - 前车安全距离 – 后车行驶距离 - 后车最小减速距离 – 后车已行驶总路程 
                        float dis = pre.Get_Total_Correction_Dis() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                                    - cur_dis - next_dis - this.Get_Total_Correction_Dis();

                        if (dis >= 0)
                        {
                            Enable3 = true;
                        }
                        else
                        {
                            Enable3 = false;
                        }
                    }
                }

                //不在同一个路口
                else 
                {
                    //车辆已进入临界区，此时需要校准total_dis，需使用Total_Correction_Dis

                    float pre_speed = pre.Get_Current_Speed();
                    float Pre_next_length = (float)(pre_speed * pre_speed / 2.0 / MainData.SimulationConfig.GetTxtMaximumAcceleration());

                    //dis = 前车已行驶总路程 + 前车最小减速距离 – 前车车长 - 前车安全距离 – 后车行驶距离 - 后车最小减速距离 – 后车已行驶总路程 
                    float dis = pre.Get_Total_Correction_Dis() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                                     - cur_dis - next_dis - this.Get_Total_Correction_Dis();

                    //对安全距离修正， 重置之前记录值-重置之后的初始值
                    float modifyDis = pre.Get_Act_Dis_Total_Correction_Old() - pre.Get_Act_Dis_Total_Correction_New();
                    dis += modifyDis;
                    //dis += 187;
                    //dis -= 2;


                    //float dis = pre.Get_Act_Dis_Total_Correction_Old() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                    //            - cur_dis - next_dis - this.Get_Total_Correction_Dis();
                    //多路口的情况 xc 如果不在同一个路口，那么需要让前车加上187
                    //if (!pre.GetIntersectionID().Equals(this.IntersectionID))
                    //{
                    //    dis += 187;
                    //    dis -= 2;
                    //}

                    if (dis >= 0)
                    {
                        Enable3 = true;
                    }
                    else
                    {
                        Enable3 = false;
                    }
                }
                
            }
            else  //没有前车，肯定不会撞车
            {
                Enable3 = true;
            }


            //判断车辆是否可以加速
            if (Enable1 && Enable2 && Enable3)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        /************************************************************************/
        /* 在匀速条件下车辆将行驶的距离，此处保证了不超速，不过线，不撞车，以供验证是否能够加速                                                                      */
        /************************************************************************/
        private bool TestConstantSpeed(float AllowedMaxSpeed, bool HaveFrontVehicle)
        {
            //当前车辆的下一个预约线
            //xc 需要修改每一条道路的deadline 
            /*
             * ????????????????????????????????
             * 需要修改
             *????????????????????????????????? 
             * */
            float deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, this.State_DeadLineMark];

            //规则一：不超速
            bool Enable1 = false;   //标识会否超速，若为true，则不会超线，可加速
            if (this.Act_Speed_Current <= AllowedMaxSpeed)
            {
                Enable1 = true;
            }
            else
            {
                Enable1 = false;
            }
            float t = 0;
            if ((this.Act_Speed_Current + Vehicle.ACCELERATION * Main.TimeStep) > AllowedMaxSpeed)
            {
                t = (AllowedMaxSpeed - this.Act_Speed_Current) / Vehicle.ACCELERATION;
            }
            else
            {
                t = Main.TimeStep;
            }
            if (t < 0)
                t = 0;

            //规则二：不超线
            bool Enable2 = false;   //标识会否超线，若为true，则不会超线，可加速
            //加速条件下，当前时间片结束时，车辆的速度
            float speed_next = this.Act_Speed_Current + Vehicle.ACCELERATION * t;
            //加速条件下，当前时间片所走过的距离
            float cur_dis = this.Act_Speed_Current * t + 0.5f * Vehicle.ACCELERATION * t * t + AllowedMaxSpeed * (Main.TimeStep - t);
            //加速条件下，当前时间片完成后，最小减速距离
            float next_dis = speed_next * speed_next / (-2.0f * Vehicle.DECCELERATION);
            if ((deadline - this.Act_Distance_Total - cur_dis - next_dis - Vehicle.SAFE_DISTANCE) > 0.00000001)    //  因为在一个时间片中，加速度固定，所以当加速时，一个时间片后的速度固定，即只需剩下的距离满足减速需求即可
            {//如果在临界区之前能够刹车完成，则加速至最大速度
                Enable2 = true;
            }
            else
            {
                Enable2 = false;
            }

            //规则三：不撞车
            bool Enable3 = false;   //标识会否撞车，若为true，则不会撞车，可加速
            if (HaveFrontVehicle)
            {             
                Vehicle pre = this.Get_FrontVehicle();
                
                //同一路口
                if (pre.GetIntersectionID().Equals(this.IntersectionID))
                {
                    if (this.State_Arrive == Definitions.ArrivePreSections)
                    {
                        //车辆未进入临界区，此时无需校准total_dis，无需使用Total_Correction_Dis                    
                        //Vehicle pre = this.Get_FrontVehicle();
                        float pre_speed = pre.Get_Current_Speed();
                        float Pre_next_length = (float)(pre_speed * pre_speed / 2.0 / MainData.SimulationConfig.GetTxtMaximumAcceleration());

                        //dis = 前车已行驶总路程 + 前车最小减速距离 – 前车车长 - 前车安全距离 – 后车行驶距离 - 后车最小减速距离 – 后车已行驶总路程 
                        float dis = pre.Get_Total_Distance() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                                    - cur_dis - next_dis - this.Act_Distance_Total;

                        //多路口的情况 xc 如果不在同一个路口，那么需要让前车加上187 未转向之前不可能路口不同
                        //if (!pre.GetIntersectionID().Equals(this.IntersectionID))
                        //{
                        //    dis += 187;
                        //    dis -= 2;   //坐标重置时存在数值误差
                        //}

                        if (dis >= 0)
                        {
                            Enable3 = true;
                        }
                        else
                        {
                            Enable3 = false;
                        }
                    }
                    else
                    {
                        //车辆已进入临界区，此时需要校准total_dis，需使用Total_Correction_Dis
                        //Vehicle pre = this.Get_FrontVehicle();
                        float pre_speed = pre.Get_Current_Speed();
                        float Pre_next_length = (float)(pre_speed * pre_speed / 2.0 / MainData.SimulationConfig.GetTxtMaximumAcceleration());

                        //dis = 前车已行驶总路程 + 前车最小减速距离 – 前车车长 - 前车安全距离
                        //      – 后车行驶距离 - 后车最小减速距离 – 后车已行驶总路程 
                        float dis = pre.Get_Total_Correction_Dis() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                                    - cur_dis - next_dis - this.Get_Total_Correction_Dis();

                       

                        //多路口的情况 xc 如果不在同一个路口，那么需要让前车加上187
                        //if (!pre.GetIntersectionID().Equals(this.IntersectionID))
                        //{
                        //    //安全距离加上车长
                        //    //this.LeastDistance = Vehicle.MAX_SPEED_TAIL_SAFE_DISTANCE + v.CarLENGTH;
                        //    if (this.IntersectionNumber == 1)
                        //    {
                        //        int a = 0;
                        //    }

                        //    dis += 187;
                        //    dis -= 2;
                        //}

                        if (dis >= 0)
                        {
                            Enable3 = true;
                        }
                        else
                        {
                            Enable3 = false;
                        }
                    }
                }

                //不在同一个路口
                else
                {
                    //车辆已进入临界区，此时需要校准total_dis，需使用Total_Correction_Dis
                    //Vehicle pre = this.Get_FrontVehicle();
                    float pre_speed = pre.Get_Current_Speed();
                    float Pre_next_length = (float)(pre_speed * pre_speed / 2.0 / MainData.SimulationConfig.GetTxtMaximumAcceleration());

                    //dis = 前车已行驶总路程 + 前车最小减速距离 – 前车车长 - 前车安全距离
                    //      – 后车行驶距离 - 后车最小减速距离 – 后车已行驶总路程 
                    //float dis = pre.Get_Total_Correction_Dis() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                    //            - cur_dis - next_dis - this.Get_Total_Correction_Dis();

                    //dis += 187;
                    //dis -= 2;


                    float dis = pre.Get_Total_Correction_Dis() + Pre_next_length - pre.CarLENGTH - Vehicle.FRONT_SAFE_DISTANCE
                                    - cur_dis - next_dis - this.Get_Total_Correction_Dis();

                    //对安全距离修正， 重置之前记录值-重置之后的初始值
                    float modifyDis = pre.Get_Act_Dis_Total_Correction_Old() - pre.Get_Act_Dis_Total_Correction_New();
                    dis += modifyDis;

                    //dis+=pre.Get_Act_Dis_Total_Correction_Old()-pre.
                    if (dis >= 0)
                    {
                        Enable3 = true;
                    }
                    else
                    {
                        Enable3 = false;
                    }
                }



      
            }
            else
            {
                Enable3 = true;
            }


            //判断是否可以加速
            if (Enable1 && Enable2 && Enable3)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /************************************************************************/
        /* 设置车辆加速度                                                                     */
        /************************************************************************/
        private void setAcceleration(float Acceleration)
        {
            this.Act_Acceleration_Current = Acceleration;
            //每一次加速度改变的情况下，需要更新到达时间，只在已经第一次发送预约消息，且在进入最后的临界区之间
        }


        /************************************************************************/
        /* 计算当下时间片中车辆的行进距离，最高速度不会超过MaxSpeed                                                                     */
        /************************************************************************/
        private void computeDrivingDistance(float AllowedMaxSpeed)
        {
            float f, t;
            float Time = Main.TimeStep;
            if (this.Act_Acceleration_Current > 0)
            {

                //计算能够加速的时间t
                if ((this.Act_Speed_Current + Time * this.Act_Acceleration_Current) > AllowedMaxSpeed)
                {
                    t = (AllowedMaxSpeed - this.Act_Speed_Current) / this.Act_Acceleration_Current;
                }
                else
                {
                    t = Time;
                }
                if (t < 0.0f)
                {
                    t = 0.0f;
                }
                t = (float)Math.Round(t, Definitions.Precise);
                //计算所走距离
                f = this.Act_Speed_Current * t + 0.5f * this.Act_Acceleration_Current * t * t + AllowedMaxSpeed * (Time - t);


                //更新车辆当前车辆的参数，即完成当前状态下车辆的运动
                this.Act_Speed_Current += this.Act_Acceleration_Current * t;
                if (this.Act_Speed_Current > AllowedMaxSpeed)
                {
                    this.Act_Speed_Current = AllowedMaxSpeed;
                }
                if (f < 0)
                {
                    f = 0;
                }
                this.Act_Distance_Current = f;
                this.Act_Distance_Total += this.Act_Distance_Current;
                this.Act_Distance_Total = (float)Math.Round(this.Act_Distance_Total, Definitions.Precise);
                this.Act_Distance_Current = (float)Math.Round(this.Act_Distance_Current, Definitions.Precise);
                this.Act_Speed_Current = (float)Math.Round(this.Act_Speed_Current, Definitions.Precise);
                this.Update_Act_Dis_Total_Correction();
            }
            else if (this.Act_Acceleration_Current < 0)
            {
                //计算减速t
                if ((this.Act_Speed_Current + Time * this.Act_Acceleration_Current) < 0.0f)
                {
                    t = (0.0f - this.Act_Speed_Current) / this.Act_Acceleration_Current;
                    if (t < 0.0f)
                    {
                        t = t * (-1.0f);
                    }
                }
                else
                {
                    t = Time;
                }
                if (t < 0.0f)
                {
                    t = 0.0f;
                }
                t = (float)Math.Round(t, Definitions.Precise);
                //计算减速距离
                f = this.Act_Speed_Current * t + 0.5f * this.Act_Acceleration_Current * t * t;

                //更新车辆当前车辆的参数，即完成当前状态下车辆的运动
                this.Act_Speed_Current += this.Act_Acceleration_Current * t;
                if (this.Act_Speed_Current < 0.0f)
                {
                    this.Act_Speed_Current = 0.0f;
                }
                if (f < 0)
                {
                    f = 0;
                }

                this.Act_Distance_Current = f;                                                                  //车辆的在一个时间片段中所走过的距离
                this.Act_Distance_Total += this.Act_Distance_Current;                                           //车辆的所走的总路程
                this.Act_Distance_Total = (float)Math.Round(this.Act_Distance_Total, Definitions.Precise);      //车辆的所走的总路程
                this.Act_Distance_Current = (float)Math.Round(this.Act_Distance_Current, Definitions.Precise);  //车辆的在一个时间片段中所走过的距离
                this.Act_Speed_Current = (float)Math.Round(this.Act_Speed_Current, Definitions.Precise);        //车辆的当前速度
                this.Update_Act_Dis_Total_Correction();                                                         //车辆已目标车道为基准线的总路程
            }
            else
            {
                this.Act_Distance_Current = this.Act_Speed_Current * Time;  //车辆的在一个时间片段中所走过的距离
                this.Act_Distance_Total += this.Act_Distance_Current;       //车辆的所走的总路程
                this.Act_Distance_Total = (float)Math.Round(this.Act_Distance_Total, Definitions.Precise); //车辆的所走的总路程
                this.Act_Distance_Current = (float)Math.Round(this.Act_Distance_Current, Definitions.Precise);
                this.Update_Act_Dis_Total_Correction();      //车辆已目标车道为基准线的总路程
            }
            //如果是在转弯位置下，还需更新角度等值Current_Radian
            this.State_IsDisUpadated = true;
        }

        /************************************************************************/
        /* 更新 Act_Dis_Total_Correction     校准行驶距离                                                               */
        /************************************************************************/
        private void Update_Act_Dis_Total_Correction()
        {
            if (this.Act_Direction_Origin != Definitions.Straight)
            {
                this.Act_Dis_Total_Correction = this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.Turning]
                                                + Coordinate.CorrectionDis[this.CarType, this.Act_Direction_Origin];

                //this.Act_Distance_Total_Old += this.Act_Distance_Total - this.Act_Distance_Total_Old_Benchmark;
                //this.Act_Dis_Total_Correction_Old += this.Act_Distance_Total - this.Act_Distance_Total_Old_Benchmark; 
            }
            else
            {
                this.Act_Dis_Total_Correction = this.Act_Distance_Total - 97.0f
                                                + Coordinate.CorrectionDis[this.CarType, this.Act_Direction_Origin];

                //this.Act_Distance_Total_Old += this.Act_Distance_Total - this.Act_Distance_Total_Old_Benchmark;
                //this.Act_Dis_Total_Correction_Old += this.Act_Distance_Total - this.Act_Distance_Total_Old_Benchmark; 
            }
        }


        /************************************************************************/
        /* 更新当前坐标值，用以实现车辆的显示功能                                                                     */
        /************************************************************************/
        private void coordinateUpdate()
        {
            //更新Turn_State标志
            try
            {
                this.UpdateTurn_State();          //更新车辆的转弯状态
            }
            catch (System.Exception e)
            {
                Console.Write(e.ToString());
            }
            switch (this.State_Turn)
            {
                case Definitions.PreTurn:
                    this.SetCoordinatesStraight();
                    break;

                case Definitions.Turning:
                    //更新角度
                    if (this.Act_Direction_Origin != Definitions.Straight)
                        this.SetCoordinatesTurn();
                    else
                        this.SetCoordinatesStraight();
                    break;

                case Definitions.AfterTurn:
                    this.SetCoordinatesStraight();
                    break;
            }

        }


        /************************************************************************/
        /* 更新当前车辆的转弯位置标志Turn_State的标志状态                                                                     */
        /************************************************************************/
        private void UpdateTurn_State()
        {
           
                //确定车辆的转弯状态
                if (this.Act_Direction_Origin != Definitions.Straight)
                {

                    //车辆的转弯线        TurnState标识当前车辆的转弯线
                    if ((this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.PreTurn]) < 0.00000000001f)
                    {
                        this.State_Turn = Definitions.PreTurn;    //车辆转弯前
                    }
                    else if ((this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.PreTurn]) >= 0.00000000001f
                             && (this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.Turning]) < 0.00000000001f)
                    {
                        this.State_Turn = Definitions.Turning;     //车辆转弯中
                    }
                    else if ((this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.Turning]) >= 0.00000000001f)
                    {
                        this.State_Turn = Definitions.AfterTurn;  //车辆转弯后                    
                    }
                }            
            
                //车辆处于车道的位置
                float arriveline1 = Coordinate.Position[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, 1];
                float arriveline2 = Coordinate.Position[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, 2];
                float leaveline1 = Coordinate.Position[this.CarType, this.Act_Direction_Origin, Definitions.Leave, 1];
                float leaveline2 = Coordinate.Position[this.CarType, this.Act_Direction_Origin, Definitions.Leave, 2];
                float leaveline3 = Coordinate.IntersactionDelLine;

                //车辆的到达情况
                if ((this.Act_Distance_Total - arriveline1) < 0.00000000001f)
                {
                    this.State_Arrive = Definitions.ArrivePreSections;    //车辆在进入初始车道，未进入到临界区
                }
                else if (((this.Act_Distance_Total - arriveline1) > 0.000000001f) && ((this.Act_Distance_Total - arriveline2) < 0.00000000001f))
                {
                    this.State_Arrive = Definitions.ArriveSections;      //车辆进入临界区
                }
                else
                {
                    this.State_Arrive = Definitions.ArriveAfterSections;   //车辆进入目标车道
                }

                //车辆的离开情况
                if ((this.Act_Distance_Total - leaveline1) < 0.00000000001f)
                {
                    this.State_Leave = Definitions.LeaveInit;             //车辆未离开初始车道
                }
                else if (((this.Act_Distance_Total - leaveline1) >= 0.00000000001f) && ((this.Act_Distance_Total - leaveline2) < 0.00000000001f))
                {
                    this.State_Leave = Definitions.LeavePreSections;      //车辆离开初始车道
                }
                else if (((this.Act_Distance_Total - leaveline2) >= 0.00000000001f) && ((this.Act_Distance_Total - leaveline3) < 0.00000000001f))
                {
                    this.State_Leave = Definitions.LeaveSections;         //车辆离开临界区
                }
                else
                {
                    this.State_Leave = Definitions.LeaveAfterSections;   //车辆离开目标车道
                }     


                //将进入目标车道的车辆的加入到目标车道队列中
                if ((this.Act_Distance_Total - arriveline2) > 0.0000000001f && (this.Act_Distance_Total - arriveline2) < 2.0f)
                {

                    if (AtTurnLaction) 
                    {

                        //转向完成xc修改删除等
                        this.LaneID = this.LaneIDDestination;
                        this.LaneID_Previous = this.LaneID_Current;
                        this.LaneID_Current = this.LaneID_Next;

                        Lane LanePreTemp = Lane.FindLaneByID(this.LaneID_Previous);   //获取之前Lane             
                        List<Vehicle> VehiclePreTemp = LanePreTemp.GetVehicleList();  //获取List删除队列
                        VehiclePreTemp.Remove(this);                                  //删除Vehicle    
                        LanePreTemp.SetVehicleList(VehiclePreTemp);                   //将通过队列写回去
                        MainData.UpdateLaneList(LanePreTemp);                         //将对象LaneTemp写回去 同步更新


                        Lane LaneCurTemp = Lane.FindLaneByID(this.LaneID_Current);    //获取现在所在Lane
                        List<Vehicle> VehicleCurTemp = LaneCurTemp.GetVehicleList();
                        
                        //this.updateFrontVehicle(4); 先不更新前车，在之后加入新
                        VehicleCurTemp.Add(this);
                        LaneCurTemp.SetVehicleList(VehicleCurTemp);
                        MainData.UpdateLaneList(LaneCurTemp);

                        AtTurnLaction = false;
                    }                 

                }                     

        }


        //判断是否结束
        private void isVehicleEnd() 
        {
            //判断是否已经结束，该车辆的
            if (this.LaneID_Current.Equals(this.LaneID_Final))
            {
                //超出距离让他消失
                if (this.Get_X(1) > 187 + Definitions.Small_Length)
                {
                    //已经到达目的地，在相应的队列中删除车辆
                    Lane LanePreTemp = Lane.FindLaneByID(this.LaneID_Current);   //获取之前Lane             
                    List<Vehicle> VehiclePreTemp = LanePreTemp.GetVehicleList();  //获取List删除队列
                    VehiclePreTemp.Remove(this);                                  //删除Vehicle    
                    LanePreTemp.SetVehicleList(VehiclePreTemp);                   //将通过队列写回去
                    MainData.UpdateLaneList(LanePreTemp);                         //将对象LaneTemp写回去 同步更新
                    //MainData.Map.GlobalVehicleList.Remove(this);                  //全局删掉
                    Main.RemoveVehicleListFromGlobal.Add(this);

                }

                //超出距离让他消失
                if (this.Get_X(1)+ Definitions.Small_Length < 0)
                {
                    //已经到达目的地，在相应的队列中删除车辆
                    Lane LanePreTemp = Lane.FindLaneByID(this.LaneID_Current);   //获取之前Lane             
                    List<Vehicle> VehiclePreTemp = LanePreTemp.GetVehicleList();  //获取List删除队列
                    VehiclePreTemp.Remove(this);                                  //删除Vehicle    
                    LanePreTemp.SetVehicleList(VehiclePreTemp);                   //将通过队列写回去
                    MainData.UpdateLaneList(LanePreTemp);                         //将对象LaneTemp写回去 同步更新
                    //MainData.Map.GlobalVehicleList.Remove(this);                  //全局删掉
                    Main.RemoveVehicleListFromGlobal.Add(this);

                }

                //超出距离让他消失
                if (this.Get_Y(1) > 187 + Definitions.Small_Length)
                {
                    //已经到达目的地，在相应的队列中删除车辆
                    Lane LanePreTemp = Lane.FindLaneByID(this.LaneID_Current);   //获取之前Lane             
                    List<Vehicle> VehiclePreTemp = LanePreTemp.GetVehicleList();  //获取List删除队列
                    VehiclePreTemp.Remove(this);                                  //删除Vehicle    
                    LanePreTemp.SetVehicleList(VehiclePreTemp);                   //将通过队列写回去
                    MainData.UpdateLaneList(LanePreTemp);                         //将对象LaneTemp写回去 同步更新
                    //MainData.Map.GlobalVehicleList.Remove(this);                  //全局删掉
                    Main.RemoveVehicleListFromGlobal.Add(this);
                }

                //超出距离让他消失
                if (this.Get_Y(1) + Definitions.Small_Length < 0)
                {
                    //已经到达目的地，在相应的队列中删除车辆
                    Lane LanePreTemp = Lane.FindLaneByID(this.LaneID_Current);   //获取之前Lane             
                    List<Vehicle> VehiclePreTemp = LanePreTemp.GetVehicleList();  //获取List删除队列
                    VehiclePreTemp.Remove(this);                                  //删除Vehicle    
                    LanePreTemp.SetVehicleList(VehiclePreTemp);                   //将通过队列写回去
                    MainData.UpdateLaneList(LanePreTemp);                         //将对象LaneTemp写回去 同步更新
                    //MainData.Map.GlobalVehicleList.Remove(this);                  //全局删掉
                    Main.RemoveVehicleListFromGlobal.Add(this);
                }
            }
            //如果没有结束
            else 
            {
                ResetVehicle();
                ////xc 拿取前车
                //Vehicle v = this.FrontVehicle;
                //if (v == null)
                //{
                //    
                //}
                //else 
                //{
                //    //安全距离加上车长
                //    double LeastDistance = Vehicle.MAX_SPEED_TAIL_SAFE_DISTANCE + v.CarLENGTH;

                //    //离开的距离大于安全距离加车长即可发车，如果不满足条件，不会发车
                //    if ((v.Get_Total_Distance() - LeastDistance) < 0.00000001)
                //    {
                //        //不能加入
                //        this.State_Current = Definitions.Parking;
                //    }
                //    else 
                //    {
                //        this.State_Current = Definitions.Entering;
                //        ResetVehicle();
                //    }

                //}           

            }
        }

        //经过中间点,重置Vehicle
        private void ResetVehicle() 
        {            
            //重置之前，保存一些行驶距离的参数

            this.Act_Distance_Total_Old = this.Act_Distance_Total;              //在车里到达下一个路口时，存放旧的车辆行驶总路程
            this.Act_Dis_Total_Correction_Old = this.Act_Dis_Total_Correction;  //在车里到达下一个路口时，矫正行驶距离

            AtTurnLaction = true;

            //设置下一步变量
            this.IntersectionID = this.IntersectionNextID;
            this.IntersectionNumber = this.IntersectionNextNumber;


            //判断是否走到最后一个路口
            if (this.IntersectionID.Equals(this.IntersectionFinalID))
            {
                this.LaneIDOrigin = int.Parse(LaneID_Current[LaneID_Current.Length - 2].ToString());
                //根据当然车道，下一个路口，计算下一步车道
                this.LaneID_Next = this.LaneID_Final;
                this.LaneIDDestination = int.Parse(LaneID_Next[LaneID_Next.Length - 2].ToString());   //目标的下一个车道 定义为1,2,3,4               
            }
            //不是最后一个路口
            else 
            {
                //记录当前车辆走到哪里
                int NowLocation = -1;
                for (int i = 0; i < IntersectionPath.Count; i++)
                {
                    if (this.IntersectionID.Equals(this.IntersectionPath[i]))
                    {
                        NowLocation = i;
                    }
                }


                //设置下一个目标车道
                if (NowLocation != -1 && NowLocation < this.IntersectionPath.Count - 1)
                {
                    this.IntersectionNextID = this.IntersectionPath[NowLocation + 1];
                    this.IntersectionNextNumber = int.Parse(IntersectionNextID.Substring(3, 2));
                }

                this.LaneID = int.Parse(LaneID_Current[LaneID_Current.Length - 2].ToString());
                this.LaneIDOrigin = int.Parse(LaneID_Current[LaneID_Current.Length - 2].ToString());

                
                
                //根据当然车道，下一个路口，计算下一步车道
                try
                {
                    this.LaneID_Next = MainData.FindLaneByInterID(this.IntersectionID, this.IntersectionNextID);
                }
                catch (Exception e)
                {

                }                
                this.LaneIDDestination = int.Parse(LaneID_Next[LaneID_Next.Length - 2].ToString());   //目标的下一个车道 定义为1,2,3,4               

            }
            


            
            this.Act_Direction_Origin = Directions.TurningDirection[this.LaneIDOrigin, this.LaneIDDestination];

            this.SCS_SectionID = SCS_ID.SCS_IDs[this.CarType, this.LaneID, this.LaneIDDestination]; ;     //将要占用的车道
            this.Act_Radius = Coordinate.Radius[this.CarType];  //车辆转弯半径，大车小车的转弯半径不同
            this.Act_Radius = Coordinate.Radius[this.CarType];  //车辆转弯半径
            this.Sche_Priority_Current = this.Sche_Priority_Origin;   //当前的优先级，可变动*/
            //this.Act_Speed_Current = Vehicle.MAX_SPEED;    //车辆速度
            //this.Act_Acceleration_Current = 0;      //加速度
            this.Act_Distance_Current = 0;          //车辆的5个点在一个时间片段中所走过的距离
            //this.Act_Distance_Total -= 187;         //车辆的5个点的所走的总路程

            this.State_Current = Definitions.Entering; //车辆所处状态
            this.Act_Direction_Current = this.Act_Direction_Origin;
            this.Act_Radian_Current = 0.0f;     //当前转角

            this.SCS_Count = this.SCS_SectionID.Length;
            this.HasToken = new bool[5] { false, false, false, false, false };   //标志车辆是否获得临界区


            //this.Front_ID = null;   /*前车的ID（跟车时使用）*/
            //this.FrontVehicle = null;       //前车
            //this.FrontNewVehicle = null;    //换道之后的新的前车
            //this.FrontVehicleChanged = false;   //标识是否出现新的前车

            this.State_AlreadyTS = new bool[5] { false, false, false, false, false };
            this.State_AlreadyTE = new bool[5] { false, false, false, false, false };
            this.State_AlreadyLeave = false;
            this.TExpectedTs = new float[5] { 0, 0, 0, 0, 0 };  //理想到达各临界区的时间，空间为5，顺序为1-4
            this.TRealTs = new float[5] { 0, 0, 0, 0, 0 };      //实际到达各临界区的时间，空间为5，顺序为1-4
            this.TExpectedTe = new float[5] { 0, 0, 0, 0, 0 };  //理想离开各临界区的时间，空间为5，顺序为1-4
            this.TRealTe = new float[5] { 0, 0, 0, 0, 0 };      //实际离开各临界区的时间，空间为5，顺序为1-4
            this.TIdealTs = new float[5] { 0, 0, 0, 0, 0 };     //车辆在无堵塞的情况下（最高速度）的情况下，到达各临界区的时间
            this.TIdealTe = new float[5] { 0, 0, 0, 0, 0 };     //车辆在无堵塞的情况下（最高速度）的情况下，离开各临界区的时间


            this.SpeedRecordQueue = new Dictionary<float, float[]>();        //每个TimeStep中记录当前车辆的速度，以备后续程序显示

            this.TimeInsert = 0.0f;         //车辆的插入时间，用以控制车辆的插入时间
            this.TimePreRecord = 0.0f;      //用以记录该车辆上一次动作的时刻，若现在 (总体时间 - 上次动作时间 >= 时间片段)，则可进行下一次动作

            //this.State_InLane = false;      //标识车辆是否已经处于监控区域之中
            this.TimeEnter = 0;         //车辆进入监控区的时间
            this.TimeReservation = 0;   //车辆通过预约线的时间
            this.TimeDelete = 0;        //车辆删除时间

            this.State_DeadLineMark = 1;    //标志车辆现今处于第几个deadline之前
            this.State_Turn = Definitions.PreTurn;
            this.State_Arrive = Definitions.ArrivePreSections;
            this.State_Leave = Definitions.LeaveInit;
            this.sendMessageAllowed = false;


            //坐标矫正：问题，转向的车辆和直行车辆不在
            //西到东  X-187
            if (this.LaneID == 1)
            {
                for (int i = 1; i < 6; i++)
                {
                    this.Coor_X[i] -= 187;
                }
                //this.Act_Distance_Total -=187;
                this.Act_Distance_Total = Coor_X[1];
                this.Coor_Y = new float[6] { 0, 0, 0, 0, 0, 0 }; //车辆的Y坐标     
                this.Coor_Y[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].Y;
               
            }
           
            //北到南 Y-187
            else if (this.LaneID == 3)
            {
                for (int i = 1; i < 6; i++)
                {
                    this.Coor_Y[i] -= 187;
                }
                this.Act_Distance_Total = Coor_Y[1];
                //this.Act_Distance_Total -= 187;
                this.Coor_X = new float[6] { 0, 0, 0, 0, 0, 0 }; //车辆的X坐标
                this.Coor_X[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].X;
                
            }
              

              //东到西  X+187 Test
            else if (this.LaneID == 2)
            {
                this.Act_Distance_Total = 0;
                this.Coor_X[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].X;                              
                this.Coor_Y = new float[6] { 0, 0, 0, 0, 0, 0 }; //车辆的Y坐标     
                this.Coor_Y[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].Y;
                               
            }
                            

            //南到北 Y+187
            else if (this.LaneID == 4)
            {
                this.Act_Distance_Total = 0;
                this.Coor_X[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].X;
                this.Coor_Y = new float[6] { 0, 0, 0, 0, 0, 0 }; //车辆的Y坐标     
                this.Coor_Y[Definitions.FrontMiddle] = Coordinate.CoordinateOrigin[this.LaneIDOrigin].Y;
                               
            }


            
            this.Coor_TurnPre_X = new float[6] { 0, 0, 0, 0, 0, 0 };    //车辆转弯前的X坐标
            this.Coor_TurnPre_Y = new float[6] { 0, 0, 0, 0, 0, 0 };    //车辆转弯前的Y坐标
            this.SetCoordinatesStraight();
            this.SetTurnPreXY();
            this.State_IsDisUpadated = false;
            this.Update_Act_Dis_Total_Correction();

            this.SCS_IDs_Leave = new Queue<int>();    //存储车辆需要使用的S-CS编号
            this.SCS_Array = new int[5];  //标志临界区申请的顺序
            this.SCS_Count = 0; //车辆需申请的临界区的数量

            string strtmp = SCS_ID.SCS_IDs_Leave[this.CarType, this.LaneIDOrigin, this.LaneIDDestination];
            for (int i = 0; i < this.SCS_SectionID.Length; i++)
            {
                this.SCS_IDs_Leave.Enqueue(int.Parse(strtmp[i].ToString()));
                this.HasToken[int.Parse(this.SCS_SectionID[i].ToString())] = false;
                this.SCS_Array[this.SCS_Count] = int.Parse(this.SCS_SectionID[i].ToString());
                this.SCS_Count++;
            }

            this.Act_Distance_Total_New= Act_Distance_Total;  //重置之后
            this.Act_Dis_Total_Correction_New = Act_Dis_Total_Correction; //重置之后



             //设置车辆的所属路口                         
            Lane LaneCurTemp = Lane.FindLaneByID(this.LaneID_Current);    //获取现在所在Lane
            //Intersection intersection = Intersection.FindIntersectionByID(LaneCurTemp.GetEnterCEIntID());
            //string intersectionid = intersection.GetID();
            //this.SetIntersectionID(intersectionid);
            //int intersectionnum = int.Parse(intersectionid.Substring(3, 2));
            //this.SetIntersectionNumber(intersectionnum);


            //将Vehicle将入到对应的队列
            List<Vehicle> VehicleCurTemp = LaneCurTemp.GetVehicleList();

            int FindVeh = -1;
            for (int i = 0; i < VehicleCurTemp.Count(); i++) 
            {
                Vehicle Vetemp = VehicleCurTemp[i];
                if (Vetemp.Get_ID().Equals(this.CarID)) 
                {
                    FindVeh = i;
                    break;
                }
            }

            if (FindVeh != -1) 
            {
                VehicleCurTemp[FindVeh] = this;
            }                
            LaneCurTemp.SetVehicleList(VehicleCurTemp);
            MainData.UpdateLaneList(LaneCurTemp);


        }

        /************************************************************************/
        /* 在直线行驶的情况下，根据this.X[Definitions.FrontMiddle]与this.Y[Definitions.FrontMiddle]，更新其余车辆的X,Y信息                                                                     */
        /************************************************************************/
        private void SetCoordinatesStraight()
        {
            //Coor_X表示车辆的X坐标
            //Coor_Y表示车辆的Y坐标

            try
            {
                this.Coor_X[Definitions.FrontMiddle] += (Coordinate.CoordinateStrFactor[this.LaneID].X * this.Act_Distance_Current);  //车头中间位置
                this.Coor_Y[Definitions.FrontMiddle] += (Coordinate.CoordinateStrFactor[this.LaneID].Y * this.Act_Distance_Current);  //车头中间位置

                this.Coor_X[Definitions.FrontMiddle] = (float)Math.Round(this.Coor_X[Definitions.FrontMiddle], Definitions.Precise);  //车头中间位置
                this.Coor_Y[Definitions.FrontMiddle] = (float)Math.Round(this.Coor_Y[Definitions.FrontMiddle], Definitions.Precise);  //车头中间位置
            }
            catch (System.Exception e) 
            {
                Console.Write(e.ToString());
            }


            try
            {
                switch (this.LaneID)
                {
                    case Definitions.WtoE_Lane:
                        this.Coor_X[Definitions.FrontLeft] = this.Coor_X[Definitions.FrontMiddle];                            //车头左
                        this.Coor_Y[Definitions.FrontLeft] = this.Coor_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;        //车头左
                        this.Coor_X[Definitions.FrontRight] = this.Coor_X[Definitions.FrontMiddle];                           //车头右
                        this.Coor_Y[Definitions.FrontRight] = this.Coor_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;       //车头右
                        this.Coor_X[Definitions.BackLeft] = this.Coor_X[Definitions.FrontMiddle] - this.CarLENGTH;            //车位左
                        this.Coor_Y[Definitions.BackLeft] = this.Coor_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;         //车位左
                        this.Coor_X[Definitions.BackRight] = this.Coor_X[Definitions.FrontMiddle] - this.CarLENGTH;           //车位右
                        this.Coor_Y[Definitions.BackRight] = this.Coor_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;        //车位右
                        break;
                    case Definitions.EtoW_Lane:
                        this.Coor_X[Definitions.FrontLeft] = this.Coor_X[Definitions.FrontMiddle];                           //车头左
                        this.Coor_Y[Definitions.FrontLeft] = this.Coor_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;       //车头左
                        this.Coor_X[Definitions.FrontRight] = this.Coor_X[Definitions.FrontMiddle];
                        this.Coor_Y[Definitions.FrontRight] = this.Coor_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                        this.Coor_X[Definitions.BackLeft] = this.Coor_X[Definitions.FrontMiddle] + this.CarLENGTH;
                        this.Coor_Y[Definitions.BackLeft] = this.Coor_Y[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                        this.Coor_X[Definitions.BackRight] = this.Coor_X[Definitions.FrontMiddle] + this.CarLENGTH;
                        this.Coor_Y[Definitions.BackRight] = this.Coor_Y[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                        break;
                    case Definitions.NtoS_Lane:
                        this.Coor_X[Definitions.FrontLeft] = this.Coor_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.FrontLeft] = this.Coor_Y[Definitions.FrontMiddle];
                        this.Coor_X[Definitions.FrontRight] = this.Coor_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.FrontRight] = this.Coor_Y[Definitions.FrontMiddle];
                        this.Coor_X[Definitions.BackLeft] = this.Coor_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.BackLeft] = this.Coor_Y[Definitions.FrontMiddle] - this.CarLENGTH;
                        this.Coor_X[Definitions.BackRight] = this.Coor_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.BackRight] = this.Coor_Y[Definitions.FrontMiddle] - this.CarLENGTH;
                        break;
                    case Definitions.StoN_Lane:
                        this.Coor_X[Definitions.FrontLeft] = this.Coor_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.FrontLeft] = this.Coor_Y[Definitions.FrontMiddle];
                        this.Coor_X[Definitions.FrontRight] = this.Coor_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.FrontRight] = this.Coor_Y[Definitions.FrontMiddle];
                        this.Coor_X[Definitions.BackLeft] = this.Coor_X[Definitions.FrontMiddle] - this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.BackLeft] = this.Coor_Y[Definitions.FrontMiddle] + this.CarLENGTH;
                        this.Coor_X[Definitions.BackRight] = this.Coor_X[Definitions.FrontMiddle] + this.CarWIDTH / 2;
                        this.Coor_Y[Definitions.BackRight] = this.Coor_Y[Definitions.FrontMiddle] + this.CarLENGTH;
                        break;
                }
            }
            catch (System.Exception e)
            {
                Console.Write(e.ToString());
            }
        }

        /************************************************************************/
        /* 在曲线运动的情况下，根据 this.Total_Distance[Definitions.FrontMiddle]的值，更新车辆的所有X,Y信息                                                                     */
        /************************************************************************/
        private void SetCoordinatesTurn()
        {
            float Dis = 0;  //在当前转弯状态下走过的路程
            this.Act_Radian_Current = this.Act_Distance_Current / this.Act_Radius;
            this.Act_Radian_Current = (float)Math.Round(this.Act_Radian_Current, Definitions.Precise);
            Dis = this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.PreTurn];
            Dis = (float)Math.Round(Dis, Definitions.Precise);
            if (Dis < 0)
            {
                MessageBox.Show("SetCoordinatesTurn error");
            }

            this.Act_Radian_Total = Dis / Coordinate.Radius[this.CarType];
            this.Act_Radian_Total = (float)Math.Round(this.Act_Radian_Total, Definitions.Precise);

            float XFactor = Coordinate.CircleFactor[this.LaneIDOrigin, this.Act_Direction_Origin].X;
            float YFactor = Coordinate.CircleFactor[this.LaneIDOrigin, this.Act_Direction_Origin].Y;

            float[] r = new float[6];
            r[Definitions.FrontMiddle] = Coordinate.Radius_Vertex[this.CarType, this.Act_Direction_Origin, Definitions.FrontMiddle];
            r[Definitions.FrontLeft] = Coordinate.Radius_Vertex[this.CarType, this.Act_Direction_Origin, Definitions.FrontLeft];
            r[Definitions.FrontRight] = Coordinate.Radius_Vertex[this.CarType, this.Act_Direction_Origin, Definitions.FrontRight];
            r[Definitions.BackLeft] = Coordinate.Radius_Vertex[this.CarType, this.Act_Direction_Origin, Definitions.BackLeft];
            r[Definitions.BackRight] = Coordinate.Radius_Vertex[this.CarType, this.Act_Direction_Origin, Definitions.BackRight];

            float[] radian_temp = new float[6];
            radian_temp[Definitions.FrontMiddle] = Coordinate.TurnPreRadian[this.CarType, this.Act_Direction_Origin, Definitions.FrontMiddle];
            radian_temp[Definitions.FrontLeft] = Coordinate.TurnPreRadian[this.CarType, this.Act_Direction_Origin, Definitions.FrontLeft];
            radian_temp[Definitions.FrontRight] = Coordinate.TurnPreRadian[this.CarType, this.Act_Direction_Origin, Definitions.FrontRight];
            radian_temp[Definitions.BackLeft] = Coordinate.TurnPreRadian[this.CarType, this.Act_Direction_Origin, Definitions.BackLeft];
            radian_temp[Definitions.BackRight] = Coordinate.TurnPreRadian[this.CarType, this.Act_Direction_Origin, Definitions.BackRight];

            if (this.LaneIDOrigin == Definitions.StoN_Lane || this.LaneIDOrigin == Definitions.NtoS_Lane)
            {
                for (int i = Definitions.FrontMiddle; i <= Definitions.BackRight; i++)
                {
                    this.Coor_X[i] = this.Coor_TurnPre_X[i] + (float)(XFactor * (r[i] * Math.Cos(radian_temp[i]) - r[i] * Math.Cos(this.Act_Radian_Total + radian_temp[i])));
                    this.Coor_Y[i] = this.Coor_TurnPre_Y[i] + (float)(YFactor * (r[i] * Math.Sin(this.Act_Radian_Total + radian_temp[i]) - r[i] * Math.Sin(radian_temp[i])));
                }
            }
            else if (this.LaneIDOrigin == Definitions.WtoE_Lane || this.LaneIDOrigin == Definitions.EtoW_Lane)
            {
                for (int i = Definitions.FrontMiddle; i <= Definitions.BackRight; i++)
                {
                    this.Coor_Y[i] = this.Coor_TurnPre_Y[i] + (float)(YFactor * (r[i] * Math.Cos(radian_temp[i]) - r[i] * Math.Cos(this.Act_Radian_Total + radian_temp[i])));
                    this.Coor_X[i] = this.Coor_TurnPre_X[i] + (float)(XFactor * (r[i] * Math.Sin(this.Act_Radian_Total + radian_temp[i]) - r[i] * Math.Sin(radian_temp[i])));
                }
            }
        }


        /************************************************************************/
        /* 发送消息给指定对象                                                                     */
        /************************************************************************/
        private void sendMessage(int Type, string Sender, string Receiver, float[] Ts, float[] Te, int Priority, String SectionID, int LaneID)
        {
            //判断消息是否合法
            if ((Type - Definitions.MsgPrefix) < 1 || (Type - Definitions.MsgPrefix) > 10)
            {
                MessageBox.Show("Wrong Message Type ---- " + Type, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //构造消息对象
            CyberCarSimulation.Bean.Message m = new CyberCarSimulation.Bean.Message(Type, Sender, Receiver, Ts, Te, this.Get_Total_Distance(), Priority, SectionID, LaneID,this.Get_Current_LaneID());

            //找到当前车辆所在的进入车道，以及该车道所在的路口
            Intersection intersection = Intersection.FindIntersectionByID(this.IntersectionID); //获取当前车辆进入的路口

            //将消息放入对应路口的VtoA_Vuffer
            if (Receiver == Definitions.Agent)
            {
                List<CyberCarSimulation.Bean.Message> qs = intersection.GetVtoA_Buffer();
                qs.Add(m);
                intersection.SetVtoA_Buffer(qs);
            }

            MainData.UpdateIntersectionList(intersection);  //将数据写回IntersectionList中
        }


         /************************************************************************/
        /**
          * 车辆发出预约信息之后的状态，可以跟车行驶，最大速度CRUISE_SPEED，
          * 下一个状态为Passing_Through或Waiting状态
        */
        /************************************************************************/
        private void stateFollowingFunc()
        {
            this.updateFrontVehicle(1);
            float[] ts = new float[5] { 0, 0, 0, 0, 0 };
            float[] te = new float[5] { 0, 0, 0, 0, 0 };
            /*车辆先行驶*/
            bool send = false;
            //判断当前时间片中的车辆是否速度为0
            if (this.Act_Speed_Current <= 0)
            {
                send = true;
            }
            this.drivingFunc(Vehicle.CRUISE_SPEED);

            if (send == true)
            {
                float[] ts1 = new float[5];
                ts1 = this.computeWaitingTs(1);
                for (int i = 1; i <= SCS_Count; ++i)
                {
                    ts1[i] = Main.getGlobalTime() + ts1[i];
                    ts1[i] = (float)Math.Round(ts1[i], Definitions.Precise);
                }
                float[] te1 = new float[5];
                te1 = this.computeWaitingTe();
                for (int i = 1; i <= SCS_Count; ++i)
                {
                    te1[i] = te1[i] + ts1[i];
                    te1[i] = (float)Math.Round(te1[i], Definitions.Precise);
                }
                this.sendMessage(Definitions.MsgRenew, this.CarID, Definitions.Agent, ts1, te1, this.Sche_Priority_Current, this.SCS_SectionID, this.LaneID);
            }

            /*已经到达停车线*/
            if (this.Act_Distance_Total >= Coordinate.IntersactionStopLine)
            {
                int CSID = int.Parse(this.SCS_SectionID[0].ToString());
                if (this.HasToken[CSID] == true)
                {
                    /*获得Token，准备通过路口*/
                    this.State_Current = Definitions.Passing_Through;
                    /*发送Passing消息*/
                    this.sendMessage(Definitions.MsgPassing, this.CarID, Definitions.Agent, ts, te, this.Sche_Priority_Origin, this.SCS_SectionID[0].ToString(), this.LaneID);
                }
                else
                {
                    /*未获得Token，停车等待*/
                    this.State_Current = Definitions.Waiting;
                }
            }
        }

        /************************************************************************/
        /* 计算等待状态下车辆的理论到达时间，
         * 因为只加速一个时间片，必然不会超过限定速度，所以不考虑超速问题
         * 加速之后必然不会超过第二个临界区
         * index是标志车辆最终申请到临界区的序号
         */
        /************************************************************************/
        private float[] computeWaitingTs(int index)
        {
            float[] t = new float[5];//到达时间
            float deadline = 0;     //当前临界区的临界区线

            float deadlineStraight = 0;
            float speedStraight = 0;
            float speedTurn = 0;

            if (this.Act_Speed_Current > Vehicle.CRUISE_SPEED)
                speedStraight = Vehicle.CRUISE_SPEED;
            else
                speedStraight = this.Act_Speed_Current;
            if (this.Act_Speed_Current > Vehicle.CROSSING_SPEED)
                speedTurn = Vehicle.CROSSING_SPEED;
            else
                speedTurn = this.Act_Speed_Current;

            for (int i = index; i <= this.SCS_Count; ++i)
            {
                deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, i];
                deadlineStraight = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, 1];
                if ((this.Act_Distance_Total - deadline) < 0.000000001f)
                {
                    //total_dis是更新过之后的结果，所以需要加上一个Main.TimeStep
                    double t_stra_acc = 0;       //在直行车道中的加速时间
                    double v_stra_acc = 0;       //在直行车道中的加速速度                    
                    CanAcc(this.Act_Distance_Total, deadlineStraight, deadline, 0, ref v_stra_acc, ref t_stra_acc);
                    t[i] = (float)t_stra_acc + Main.TimeStep;
                    if (t[i] - 15000f > 0.000000001f)
                    {
                        t[i] = 15000f;
                    }
                }
                else //若总路程大于某一临界区进入线，则无需再申请该临界区
                {
                    t[i] = 0.0f;
                }
            }
            return t;
        }

        /************************************************************************/
        /* 计算等待状态下车辆的理论离开时间              */
        /************************************************************************/
        private float[] computeWaitingTe()
        {
            float deadline = 0;     //当前临界区的临界区线
            float[] t = new float[5] { 0, 0, 0, 0, 0 };//离开时间
            float deadlineStraight = 0;
            float speedStraight = 0;
            float speedTurn = 0;

            if (this.Act_Speed_Current > Vehicle.CRUISE_SPEED)
                speedStraight = Vehicle.CRUISE_SPEED;
            else
                speedStraight = this.Act_Speed_Current;
            if (this.Act_Speed_Current > Vehicle.CROSSING_SPEED)
                speedTurn = Vehicle.CROSSING_SPEED;
            else
                speedTurn = this.Act_Speed_Current;

            for (int i = 1; i <= this.SCS_Count; ++i)
            {
                deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Leave, i];
                deadlineStraight = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, 1];

                if ((this.Act_Distance_Total - deadline) < 0.000000001f)//车辆未离开临界区i
                {
                    //total_dis是更新过之后的结果，所以需要加上一个Main.TimeStep
                    double t_stra_acc = 0;       //在直行车道中的加速时间
                    double v_stra_acc = 0;       //在直行车道中的加速速度                    
                    CanAcc(this.Act_Distance_Total, deadlineStraight, deadline, 0, ref v_stra_acc, ref t_stra_acc);
                    t[i] = (float)t_stra_acc + Main.TimeStep;
                    if (t[i] - 15000f > 0.000000001f)
                    {
                        t[i] = 15000f;
                    }
                }
                else //若总路程大于某一临界区进入线，则无需再申请该临界区
                {
                    t[i] = 0.0f;
                }
            }
            return t;
        }

        /************************************************************************/
        /* 计算车辆在加速条件下到达deadline时的最大速度和所需时间
         */
        /************************************************************************/
        void CanAcc(double dis_cur, double deadline_stra, double deadline, double v1, ref double v_end, ref double t)
        {
            //拿到Cconfig配置参数
            float MaximumAcc = MainData.SimulationConfig.GetTxtMaximumAcceleration();
            float CruisingSpeed = MainData.SimulationConfig.GetTxtCruisingSpeed();
            float CrossingSpeed = MainData.SimulationConfig.GetTxtCrossingSpeed();


            //计算在直行车道中的过程
            double dis = dis_cur - deadline_stra;

            if (dis < 0)    //车辆未到达临界区范围，计算车辆时间时，需注意车辆在直行车道和临界区范围内的最大允许速度不同
            {
                double v2 = Math.Sqrt(2 * MaximumAcc * Math.Abs(dis) + v1 * v1);  //车辆最大可加速至v2
                double vtmp = 0;
                if ((v2 - CruisingSpeed) > 0.0000001)
                    vtmp = CruisingSpeed;
                else
                    vtmp = v2;
                double dis_acc = (vtmp * vtmp - v1 * v1) / 2 / MaximumAcc;
                double t_tmp = (vtmp - v1) / MaximumAcc + (dis - dis_acc) / vtmp;

                double dis2 = deadline - deadline_stra;
                double vtmp2 = 0;
                double v3 = Math.Sqrt(2 * MaximumAcc * dis2 + vtmp * vtmp);
                if ((v3 - CrossingSpeed) > 0.0000001)
                    vtmp2 = CrossingSpeed;
                else
                    vtmp2 = v3;
                double dis_acc2 = (vtmp2 * vtmp2 - vtmp * vtmp) / 2 / MaximumAcc;
                t = (vtmp2 - vtmp) / MaximumAcc + (dis2 - dis_acc2) / vtmp2;
                v_end = vtmp2;
            }
            else
            {//车辆已到达临界区范围，但未通过deadline界线，即 deadline > dis_cur
                double dis2 = deadline - dis_cur;
                double vtmp = 0;
                double v2 = Math.Sqrt(2 * MaximumAcc * Math.Abs(dis2) + v1 * v1);
                if ((v2 - CrossingSpeed) > 0.0000001)
                    vtmp = CrossingSpeed;
                else
                    vtmp = v2;
                double dis_acc = (vtmp * vtmp - v1 * v1) / 2 / MaximumAcc;
                t = (vtmp - v1) / MaximumAcc + (dis2 - dis_acc) / vtmp;
                v_end = vtmp;
            }
        }




        /************************************************************************/
        /* 车辆获得Token之后的状态，加速通过路口，车速最高为CROSSING_SPEED，
         * 下一个状态为Passed状态 
        */
        /************************************************************************/
        private void statePassingFunc()
           {
               //在转弯状态下需要更新前车
               if ((this.Act_Distance_Total - Coordinate.IntersactionEnterLine) > 0.000000001)
               {
                   this.updateFrontVehicle(2);
               }
               else
               {
                   this.updateFrontVehicle(1);
               }
               //            this.FrontVehicle = this.FrontNewVehicle; //FrontNewVehicle; 换道之后的新的前车
            ////在转弯状态下需要更新前车
            //if ((this.Act_Distance_Total - Coordinate.IntersactionEnterLine) > 0.000000001)
            //{
            //    //this.updateFrontVehicle(2);

            //    //加入下一个List之后
            //    if (this.AtTurnLaction) 
            //    {
            //        this.updateFrontVehicle(4);
            //    }
            //    //还没加入下一个List
            //    else
            //    {
            //        this.updateFrontVehicle(2);
            //    }

            //}
            //else
            //{
            //    this.updateFrontVehicle(1);
            //}
            //this.FrontVehicle = this.FrontNewVehicle;
            if (this.FrontVehicle != null)
            {
                this.Front_ID = this.FrontVehicle.Get_ID();
            }
            else
            {
                this.Front_ID = null;
            }
            int index = 1;      //车辆当前所需申请的临界区
            bool send = false;  //标志是否是当前速度为0的情况


            try {
                /************************************************************************/
                /* 对车辆转弯过程进行控制                                                                     */
                /************************************************************************/
                if (this.SCS_Count == 1)
                {
                    this.Drive_ALL_Speed_Set();
                }
                else if (this.SCS_Count == 2)
                {
                    if (this.HasToken[this.SCS_Array[1]] == false)
                    {   //drivingFunc判断能否加减速，且当前时间片应该如何行动。
                        //且根据DeadLineMark，完成车辆的刹车、前进控制
                        if (this.Act_Speed_Current == 0)
                        {
                            send = true;
                            index = 2;
                        }
                        this.Drive_Some_Speed_Set();
                    }
                    else
                    {
                        this.Drive_ALL_Speed_Set();
                    }
                }
                else if (this.SCS_Count == 3)
                {//左拐
                    if (this.HasToken[this.SCS_Array[1]] == false)
                    {
                        if (this.Act_Speed_Current == 0)
                        {
                            send = true;
                            index = 2;
                        }
                        this.Drive_Some_Speed_Set();
                    }
                    else
                    {
                        if (this.HasToken[this.SCS_Array[2]] == false)
                        {
                            if (this.Act_Speed_Current == 0)
                            {
                                send = true;
                                index = 3;
                            }
                            this.Drive_Some_Speed_Set();
                        }
                        else
                        {
                            try { 
                                this.Drive_ALL_Speed_Set();
                            }
                            catch (System.Exception e)
                            {
                                Console.Write(e.ToString());
                            }
                        }
                    }
                }
                else if (this.SCS_Count == 4)
                {
                    if (this.HasToken[this.SCS_Array[1]] == false)
                    {
                        if (this.Act_Speed_Current == 0)
                        {
                            send = true;
                            index = 2;
                        }
                        this.Drive_Some_Speed_Set();
                    }
                    else
                    {
                        if (this.HasToken[this.SCS_Array[2]] == false)
                        {
                            if (this.Act_Speed_Current == 0)
                            {
                                send = true;
                                index = 3;
                            }
                            this.Drive_Some_Speed_Set();
                        }
                        else
                        {
                            if (this.HasToken[this.SCS_Array[3]] == false)
                            {
                                if (this.Act_Speed_Current == 0)
                                {
                                    send = true;
                                    index = 4;
                                }
                                this.Drive_Some_Speed_Set();
                            }
                            else
                            {
                                this.Drive_ALL_Speed_Set();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show(this.CarID + " SCS Count is wrong ---- " + this.SCS_Count, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (System.Exception e)
            {
                Console.Write(e.ToString());
            }
        

            try 
            {
                //处理车辆在临界区之前停止但是需要申请临界区的情况
                if (send == true)
                {
                    float[] ts = new float[5];
                    ts = this.computeWaitingTs(index);
                    for (int i = 1; i < index; i++)
                    {
                        ts[i] = Main.getGlobalTime();
                        ts[i] = (float)Math.Round(ts[i], Definitions.Precise);
                    }
                    for (int i = index; i <= SCS_Count; ++i)
                    {
                        ts[i] = Main.getGlobalTime() + ts[i];
                        ts[i] = (float)Math.Round(ts[i], Definitions.Precise);
                    }
                    float[] te = new float[5];
                    te = this.computeWaitingTe();
                    for (int i = 1; i <= SCS_Count; ++i)
                    {
                        te[i] = te[i] + ts[i];
                        te[i] = (float)Math.Round(te[i], Definitions.Precise);
                    }
                    this.sendMessage(Definitions.MsgRenew, this.CarID, Definitions.Agent, ts, te, this.Sche_Priority_Current, this.SCS_SectionID, this.LaneID);
                }
            }catch(System.Exception e)
            {
                Console.Write(e.ToString());
            }

            try {

                /*进入路口后可能要修改行进方向，所在车道*/
                if (this.Act_Direction_Origin != Definitions.Straight)
                {
                    if ((this.Act_Distance_Total - Coordinate.TurnState[this.CarType, this.Act_Direction_Origin, Definitions.Turning]) > 0.00000000001f)
                    {
                        this.Act_Direction_Current = Definitions.Straight;
                        //this.LaneID = this.LaneIDDestination;
                        //this.LaneID_Current = this.LaneID_Next;
                    }
                    else
                    {
                        this.Act_Direction_Current = this.Act_Direction_Origin;
                        //this.LaneID = this.LaneIDOrigin;
                        //this.LaneID_Current = this.LaneID_Previous;
                    }
                }
                else
                {
                    this.Act_Direction_Current = Definitions.Straight;
                }

                //释放临界区资源
                float[] t1 = new float[6];
                float[] t2 = new float[6];
                float deadline = 0;
                float arriveline = 0;
                for (int i = 1; i <= this.SCS_SectionID.Count(); i++)
                {
                    arriveline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, i];
                    if ((this.Act_Distance_Total - arriveline) > 0.00000000001f)
                    {
                        if (this.State_AlreadyTS[i] == false)
                        {
                            this.TRealTs[i] = Main.getGlobalTime() + Main.TimeStep + Main.TimeStep / 2.0f;
                            this.State_AlreadyTS[i] = true;
                        }

                    }
                }
                for (int i = 1; i <= this.SCS_SectionID.Count(); i++)
                {
                    deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Leave, i];
                    if ((this.Act_Distance_Total - deadline) > 0.00000000001f)
                    {
                        if (this.State_AlreadyTE[i] == false)
                        {
                            this.sendMessage(Definitions.MsgRelease, this.CarID, Definitions.Agent, t1, t2, this.Sche_Priority_Origin, this.SCS_IDs_Leave.Peek().ToString(), this.LaneID);
                            this.TRealTe[i] = Main.getGlobalTime() + Main.TimeStep + Main.TimeStep / 2.0f;
                            this.State_AlreadyTE[i] = true;
                            this.SCS_IDs_Leave.Dequeue();
                        }
                    }
                }

                if (this.SCS_IDs_Leave.Count == 0 || (this.Act_Distance_Total - Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Leave, this.SCS_Count]) > 0.000000001f)
                {
                    this.State_Current = Definitions.Passed;
                    this.Passed_ID = Main.Passed_ID;
                    Main.Passed_ID++;
                    this.sendMessage(Definitions.MsgPassed, this.CarID, Definitions.Agent, t1, t2, this.Sche_Priority_Current, null, this.LaneIDOrigin);
                    this.IsAuth = false;
                    return;
                }
                else
                {

                }
            }
            catch (System.Exception e)
            {
                Console.Write(e.ToString());
            }

        }

        //按照车辆的到达、离开状态确定速度
        private void Drive_ALL_Speed_Set()
        {
            if (this.State_Arrive == Definitions.ArrivePreSections)     //车辆在进入初始车道，未进入到临界区，按照巡航速度行驶
            {
                this.drivingFunc(Vehicle.CRUISE_SPEED);
            }
            else if (this.State_Arrive == Definitions.ArriveSections)   //车辆进入临界区，按照通过速度行驶
            {
                this.drivingFunc(Vehicle.CROSSING_SPEED);
            }
            else if (this.State_Arrive == Definitions.ArriveAfterSections && this.State_Leave == Definitions.LeaveInit)   //车辆在进入目标车道，车辆未离开初始车道
            {
                this.drivingFunc(Vehicle.CROSSING_SPEED);
            }
            else if (this.State_Arrive == Definitions.ArriveAfterSections && this.State_Leave == Definitions.LeavePreSections)
            {
                try { 
                    this.drivingFunc(Vehicle.CROSSING_SPEED);
                }
                catch (System.Exception e)
                {
                    Console.Write(e.ToString());
                }
            }
            else if (this.State_Arrive == Definitions.ArriveAfterSections && this.State_Leave == Definitions.LeaveSections)
            {
                this.drivingFunc(Vehicle.MAX_SPEED);
            }
        }

        private void Drive_Some_Speed_Set()
        {
            if (this.State_Arrive == Definitions.ArrivePreSections)
            {
                this.drivingFunc(Vehicle.CRUISE_SPEED);
            }
            else if (this.State_Arrive == Definitions.ArriveSections)
            {
                this.drivingFunc(Vehicle.CROSSING_SPEED);
            }
            else if (this.State_Arrive == Definitions.ArriveAfterSections)
            {
                MessageBox.Show("statePassingFunc speed set error");
            }
        }


        /************************************************************************/
        /* 车辆未获得Token，且已经越过停车线，此时应该减速刹车，直到获得Token         */
        /************************************************************************/
        private void stateWaitingFunc()
        {

            this.updateFrontVehicle(1);
            bool send = false;
            float[] t1 = new float[6];
            float[] t2 = new float[6];

            int CSID = int.Parse(this.SCS_SectionID[0].ToString());
            if (this.HasToken[CSID] == false)
            {//未获得第一个临界区的授权
                if (this.Act_Speed_Current <= 0)
                {
                    send = true;
                }

                this.drivingFunc(Vehicle.CRUISE_SPEED);

                //若该车在临界区前停下（速度为0），则需要按照车辆的加速条件，对车辆信息进行更新,将之前更新的消息替换掉，以防止车辆一直等待
                //应用下面函数的情况只能是速度为0，且一旦加速，则会超线，所以此种情况，只给一个临界区的加速时间，其余的按照匀速进行计算
                if (send == true)
                {
                    float[] ts = new float[5];
                    ts = this.computeWaitingTs(1);
                    for (int i = 1; i <= SCS_Count; ++i)
                    {
                        ts[i] = Main.getGlobalTime() + ts[i];
                        ts[i] = (float)Math.Round(ts[i], Definitions.Precise);
                    }
                    float[] te = new float[5];
                    te = this.computeWaitingTe();
                    for (int i = 1; i <= SCS_Count; ++i)
                    {
                        te[i] = te[i] + ts[i];
                        te[i] = (float)Math.Round(te[i], Definitions.Precise);
                    }
                    this.sendMessage(Definitions.MsgRenew, this.CarID, Definitions.Agent, ts, te, this.Sche_Priority_Current, this.SCS_SectionID, this.LaneID);
                }
            }
            else
            {
                this.drivingFunc(Vehicle.CRUISE_SPEED);
                this.State_Current = Definitions.Passing_Through;
                this.sendMessage(Definitions.MsgPassing, this.CarID, Definitions.Agent, t1, t2, -1, this.SCS_SectionID[0].ToString(), this.LaneID);
            }
        }

        /************************************************************************/
        /* 通过路口之后的状态函数，车辆全速行进                                                                     */
        /************************************************************************/
        private void statePassedFunc()
        {
            //this.updateFrontVehicle(2);
            this.updateFrontVehicle(4);
            this.sendMessageAllowed = false;
            /*通过路口，恢复原有的优先级*/
            if (this.Sche_Priority_Current != this.Sche_Priority_Origin)
            {
                this.Sche_Priority_Current = this.Sche_Priority_Origin;
            }
            this.Act_Direction_Current = Definitions.Straight;
            this.LaneID = this.LaneIDDestination;

            this.drivingFunc(Vehicle.MAX_SPEED);
            //车辆离开临界区，则记录离开时间，在Main.cs中将该车辆删除
            if (((this.Act_Distance_Total - Coordinate.IntersactionDelLine) > 0.00000001f) && (this.State_AlreadyLeave == false))
            {
                this.TimeDelete = Main.getGlobalTime() + Main.TimeStep - Main.TimeStep * 0.50f;
                this.State_AlreadyLeave = true;
            }
        }






        /************************************************************************/
        /* 更新最理想情况下的车辆情况                                                                     */
        /************************************************************************/
        private void Update_TIdeal_GenerationTs()
        {
            for (int i = 0; i < this.SCS_SectionID.Count(); i++)
            {
                float deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, i + 1];
                float t = 0.0f;
                double dis = ((Vehicle.CROSSING_SPEED * Vehicle.CROSSING_SPEED) - (Vehicle.CRUISE_SPEED * Vehicle.CRUISE_SPEED)) / 2.0 / Vehicle.ACCELERATION;
                double time = (Vehicle.CROSSING_SPEED - Vehicle.CRUISE_SPEED) / Vehicle.ACCELERATION;
                if ((deadline - Coordinate.IntersactionEnterLine) >= dis)
                {
                    t = (float)(Coordinate.IntersationEnterTime + time + (deadline - Coordinate.IntersactionEnterLine - dis) / Vehicle.CROSSING_SPEED);
                }
                else if ((deadline - Coordinate.IntersactionEnterLine) >= -0.000001)
                {
                    double dis1 = deadline - Coordinate.IntersactionEnterLine;
                    if ((dis1 - dis) > 0.00001)
                    {
                        Debug.WriteLine("fdasfdsa");
                    }
                    double VEnd = Math.Sqrt(2 * Vehicle.ACCELERATION * dis1 + Vehicle.CRUISE_SPEED * Vehicle.CRUISE_SPEED);
                    if ((VEnd - Vehicle.CRUISE_SPEED) < -0.00000001)
                    {
                        Debug.WriteLine("fdsaf");
                    }
                    double t1 = (VEnd - Vehicle.CRUISE_SPEED) / Vehicle.ACCELERATION;
                    t = Coordinate.IntersationEnterTime + (float)t1;
                }
                else
                {
                    t = Coordinate.IntersationEnterTime;
                }
                this.TIdealTs[i + 1] = this.TimeInsert + t;
            }
        }

        /************************************************************************/
        /* 更新最理想情况下的车辆情况                                                                     */
        /************************************************************************/
        private void Update_TIdeal_GenerationTe()
        {
            for (int i = 0; i < this.SCS_SectionID.Count(); i++)
            {
                float deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Leave, i + 1];
                float t = 0.0f;
                double dis = ((Vehicle.CROSSING_SPEED * Vehicle.CROSSING_SPEED) - (Vehicle.CRUISE_SPEED * Vehicle.CRUISE_SPEED)) / 2.0 / Vehicle.ACCELERATION;
                double time = (Vehicle.CROSSING_SPEED - Vehicle.CRUISE_SPEED) / Vehicle.ACCELERATION;
                if ((deadline - Coordinate.IntersactionEnterLine) >= dis)
                {
                    t = (float)(Coordinate.IntersationEnterTime + time + (deadline - Coordinate.IntersactionEnterLine - dis) / Vehicle.CROSSING_SPEED);
                }
                else if ((deadline - Coordinate.IntersactionEnterLine) >= -0.000001)
                {
                    double dis1 = deadline - Coordinate.IntersactionEnterLine;
                    if ((dis1 - dis) > 0.00001)
                    {
                        Debug.WriteLine("fdasfdsa");
                    }
                    double VEnd = Math.Sqrt(2 * Vehicle.ACCELERATION * dis1 + Vehicle.CRUISE_SPEED * Vehicle.CRUISE_SPEED);
                    if ((VEnd - Vehicle.CRUISE_SPEED) < -0.00000001)
                    {
                        Debug.WriteLine("fdsaf");
                    }
                    double t1 = (VEnd - Vehicle.CRUISE_SPEED) / Vehicle.ACCELERATION;
                    t = Coordinate.IntersationEnterTime + (float)t1;
                }
                else
                {
                    t = Coordinate.IntersationEnterTime;
                }
                this.TIdealTe[i + 1] = this.TimeInsert + t;
            }
        }



        /************************************************************************/
        /* 更新当前车辆预约时间，以供sendmessage()调用                                                                     */
        /************************************************************************/
        private float UpdateAuthrizeTime()
        {
            float[] ts = new float[5];
            float[] te = new float[5];
            ts = this.computeTsIdeal();

            /*需要引入修正变量以便减少误差，原因不明*/
            for (int i = 1; i <= SCS_Count; ++i)
            {
                this.TExpectedTs[i] = Main.getGlobalTime() + ts[i];
                this.TExpectedTs[i] = (float)Math.Round(this.TExpectedTs[i], Definitions.Precise);
            }

            te = this.computeIdealTe();
            for (int i = 1; i <= SCS_Count; ++i)
            {
                this.TExpectedTe[i] = this.TExpectedTs[i] + te[i];
                this.TExpectedTe[i] = (float)Math.Round(this.TExpectedTe[i], Definitions.Precise);
            }
            return 0;
        }

        /************************************************************************/
        /* 计算理论到达时间，在更新本时间片运动之后计算,而非加速度改变之后直接计算                                                                     */
        /************************************************************************/
        private float[] computeTsIdeal()
        {
            float[] t = new float[5];//到达时间
            float deadline = 0;     //当前临界区的临界区线
            float deadlineStraight = 0;
            float speedStraight = 0;
            float speedTurn = 0;

            if (this.Act_Speed_Current > Vehicle.CRUISE_SPEED)
            {
                speedStraight = Vehicle.CRUISE_SPEED;
                //                 MessageBox.Show("computeTsIdeal error1");
            }
            else
            {
                speedStraight = this.Act_Speed_Current;
            }
            if (this.Act_Speed_Current > Vehicle.CROSSING_SPEED)
            {
                speedTurn = Vehicle.CROSSING_SPEED;
                //                 MessageBox.Show("computeTsIdeal error2");
            }
            else
            {
                speedTurn = this.Act_Speed_Current;
            }

            for (int i = 1; i <= this.SCS_Count; ++i)
            {
                deadline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, i];
                deadlineStraight = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, 1];
                //若总路程大于某一临界区进入线，则无需再申请
                if (this.Act_Distance_Total < deadline)
                {
                    if (this.Act_Distance_Total < deadlineStraight)
                    {
                        if (speedStraight == 0 && speedTurn == 0)
                        {
                            t[i] = 15000f;
                        }
                        else
                        {
                            //total_dis是更新过之后的结果，所以需要加上一个Main.TimeStep
                            t[i] = (deadlineStraight - this.Act_Distance_Total) / speedStraight + Main.TimeStep +
                                    (deadline - deadlineStraight) / speedTurn;
                            if (t[i] - 15000f > 0.000000001f)
                            {
                                t[i] = 15000f;
                            }
                        }
                    }
                    else
                    {
                        if (speedTurn == 0)
                        {
                            t[i] = 15000f;
                        }
                        else
                        {
                            //total_dis是更新过之后的结果，所以需要加上一个Main.TimeStep
                            t[i] = (deadline - this.Act_Distance_Total) / speedTurn + Main.TimeStep;
                            if (t[i] - 15000f > 0.000000001f)
                            {
                                t[i] = 15000f;
                            }
                        }
                    }
                }
                else
                {
                    //若下一时间片已经到达临界区，则可不再申请，且更新到达时间信息
                    //此时的时间就设定为-1.0f,最后申请的时候，可按照这个标准值对车辆进行处理
                    t[i] = -1.0f;
                }
            }
            return t;
        }

        /************************************************************************/
        /* 计算车辆在理想状态下通过路口所需要花费的时间,此时使用的速度                                                                     */
        /************************************************************************/
        private float[] computeIdealTe()
        {
            float[] t = new float[5];

            float speedTurn = 0;
            if (this.Act_Speed_Current > Vehicle.CROSSING_SPEED)
            {
                speedTurn = Vehicle.CROSSING_SPEED;
                //                 MessageBox.Show("computeIdealTe error1");
            }
            else
            {
                speedTurn = this.Act_Speed_Current;
            }

            float beginline = 0f;
            float endline = 0f;
            for (int i = 1; i <= this.SCS_Count; ++i)
            {
                beginline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Arrive, i];
                endline = Coordinate.DeadLine[this.CarType, this.Act_Direction_Origin, Definitions.Leave, i];

                if ((this.TExpectedTs[i] - Main.getGlobalTime()) < 0.0f)
                {//车辆已进入该临界区，但是还没有离开该临界区                   
                    if (this.Act_Distance_Total < endline)
                    {   //加上Main.TimeStep是因为原来的TsIdeal计算中，如果当前时间片结束的时候，该车到达临界区，则时间不计。
                        //所以当时间片结束，仍未出临界区的情况发生时，需要将忽略的时间加回。
                        if (speedTurn == 0)
                        {
                            t[i] = 15000f;
                        }
                        else
                        {
                            t[i] = (endline - this.Act_Distance_Total) / speedTurn + Main.TimeStep;
                            if (t[i] - 15000f > 0.000000001f)
                            {
                                t[i] = 15000f;
                            }
                        }
                        //因为此时的TsIdeal[i]为-1，所以需要将t[i]在原来的基础上加上1
                        t[i] = t[i] + 1;
                    }
                    else
                    {//车辆已经离开该临界区
                        t[i] = 0f;
                    }
                }
                else
                {//车辆未进入该临界区
                    if (speedTurn == 0)
                    {
                        t[i] = 15000f;
                    }
                    else
                    {
                        t[i] = (endline - beginline) / speedTurn;
                    }
                    if (t[i] - 15000f > 0.000000001f)
                    {
                        t[i] = 15000f;
                    }
                }
            }
            return t;
        }         

        public string Get_ID()
        {
            return this.CarID;
        }

        //所属的路口IntersectionID
        public string GetIntersectionID()
        {
            return this.IntersectionID; 
        }
        public void SetIntersectionID(string IntersectionID)
        {
            this.IntersectionID = IntersectionID; 
        }
        public int GetIntersectionNumber()
        {
            return this.IntersectionNumber;
        }
        public void SetIntersectionNumber(int IntersectionNumber)
        {
            this.IntersectionNumber = IntersectionNumber;
        }

        //所属下一个路口IntersectionNextID
        public string GetIntersectionNextID() 
        {
            return this.IntersectionNextID;
        }
        public void SetIntersectionNextID(string IntersectionNextID) 
        {
            this.IntersectionNextID = IntersectionNextID;
        }
        public int GetIntersectionNextNumber() 
        {
            return this.IntersectionNextNumber;
        }
        public void SetIntersectionNextNumber(int IntersectionNextNumber) 
        {
            this.IntersectionNextNumber = IntersectionNextNumber;
        }

        //最终的路口
        public string GetIntersectionFinalID()
        {
            return this.IntersectionFinalID;
        }
        public void SetIntersectionFinalID(string IntersectionFinalID) 
        {
            this.IntersectionFinalID = IntersectionFinalID;
        }
        public int GetIntersectionFinalNumber()
        {
            return this.IntersectionFinalNumber;
        }
        public void SetIntersectionFinalNumber(int IntersectionFinalNumber) 
        {
            this.IntersectionFinalNumber = IntersectionFinalNumber;
        }

        //获取车辆路径
        public string GetPathNumber() 
        {
            return this.PathNumber;
        }
        //设置车辆路径
        public void SetPathNumber(string PathNumber) 
        {
            this.PathNumber = PathNumber;
        }
      
               
        //获取路径
        public string[] GetVehiclePath() 
        {
            return this.VehiclePath;
        }

        

        //设置路径
        public void SetVehiclePath(string[] VehiclePath) 
        {
            this.VehiclePath = VehiclePath;
        }

        //保证兼容性
        public int Get_LaneIDOrigin() 
        {
            return this.LaneIDOrigin;        //原车道
        }
        public int Get_LaneIDFinal() 
        {
            return this.LaneIDFinal;
        }
        public int Get_LaneID()
        {
            return this.LaneID;              //现在车道
        }
        public int Get_LaneIDDestination()
        {
            return this.LaneIDDestination;  //目标车道
        }

        public string Get_Previous_LaneID() 
        {
            return this.LaneID_Previous;  //返回原来车道
        }

        public string Get_Origin_LaneID()    //返回原生成在车道
        {
            return this.LaneID_Origin;
        }
        public string Get_Current_LaneID()   //返回当前所在车道
        {
            return this.LaneID_Current;
        }
        
        public string Get_Final_LaneID()    //返回最终目的车道
        {
            return this.LaneID_Final;
        }

        public int Get_Origin_Priority()
        {
            return this.Sche_Priority_Origin;
        }
        public string Get_SectionID()
        {
            return this.SCS_SectionID;
        }
        public bool Get_AlreadyTS(int i)
        {
            return this.State_AlreadyTS[i];
        }
        public bool Get_AlreadyTE(int i)
        {
            return this.State_AlreadyTE[i];
        }
        public int Get_Current_Priority()
        {
            return this.Sche_Priority_Current;
        }
        public float Get_Current_Speed()
        {
            return this.Act_Speed_Current;
        }
        public float Get_Current_Acceleration()
        {
            return this.Act_Acceleration_Current;
        }
        public float Get_Current_Distance()
        {
            return this.Act_Distance_Current;
        }
        public float Get_Total_Distance()
        {
            return this.Act_Distance_Total;
        }
        public float Get_X(int i)
        {
            return this.Coor_X[i];
        }
        public float Get_Y(int i)
        {
            return this.Coor_Y[i];
        }
        public int Get_Current_State()          //车辆所处状态
        {
            return this.State_Current;
        }
        public int Get_Current_Direction()      //车辆当前方向
        {
            return this.Act_Direction_Current;
        }
        public float Get_Current_Radian()       //当前转角
        {
            return this.Act_Radian_Current;
        }
        public bool Get_HasToken(int i)            //标志车辆是否获得临界区
        {
            return this.HasToken[i];
        }
        public string Get_Front_ID()            /*前车的ID（跟车时使用）*/
        {
            return this.Front_ID;
        }
        public Vehicle Get_FrontVehicle()       //前车
        {
            return this.FrontVehicle;
        }
        public Vehicle Get_NewFrontVehicle()    //换道之后的新的前车
        {
            return this.FrontNewVehicle;
        }
        public bool Get_FrontVehicleChanged()   //标识是否出现新的前车
        {
            return this.FrontVehicleChanged;
        }
        public float Get_TsIdeal(int i)            //理想到达各临界区的时间，空间为5，顺序为1-4
        {
            return this.TExpectedTs[i];
        }
        public float Get_RealTs(int i)             //实际到达各临界区的时间，空间为5，顺序为1-4
        {
            return this.TRealTs[i];
        }
        public float Get_IdealTe(int i)            //理想离开各临界区的时间，空间为5，顺序为1-4
        {
            return this.TExpectedTe[i];
        }
        public float Get_RealTe(int i)             //实际离开各临界区的时间，空间为5，顺序为1-4
        {
            return this.TRealTe[i];
        }
        public float Get_TsIdeal_Generation(int i)   //车辆在无堵塞的情况下（最高速度）的情况下，到达各临界区的时间
        {
            return this.TIdealTs[i];
        }
        public float Get_IdealTe_Generation(int i)   //车辆在无堵塞的情况下（最高速度）的情况下，离开各临界区的时间
        {
            return this.TIdealTe[i];
        }

        public Queue<int> Get_SCS_IDs(int i)     //存储车辆需要使用的S-CS编号，
        {
            return this.SCS_IDs_Leave;
        }
        public int Get_SCS_Count()          //车辆需申请的临界区的数量
        {
            return this.SCS_Count;
        }
        public float Get_GenerationTime()  //车辆生成时间
        {
            return this.GenerationTime;
        }
       
        public float Get_TimeInsert()
        {
            return this.TimeInsert;
        }
        public float Get_TimeEnter()
        {
            return this.TimeEnter;
        }
        public float Get_TimeReservation()
        {
            return this.TimeReservation;
        }
        public float Get_TimeDelete()
        {
            return this.TimeDelete;
        }
        public int Get_PassedID()
        {
            return this.Passed_ID;
        }
        public int Get_Cartype()
        {
            return this.CarType;
        }
        public int Get_SpeedRecordLength()
        {
            return this.SpeedRecordQueue.Count;
        }
        public float Get_SpeedRecordElementKey(int Index)
        {
            return this.SpeedRecordQueue.ElementAt(Index).Key;
        }
        public float[] Get_SpeedRecordElementValue(int Index)
        {
            return this.SpeedRecordQueue.ElementAt(Index).Value;
        }

       

        public void Set_TimeInsert(float time)  //设置
        {
            this.TimeInsert = time;
            this.Update_TIdeal_GenerationTs();
            this.Update_TIdeal_GenerationTe();
        }
        public int Get_Turn_State()
        {
            return this.State_Turn;
        }
        public int Get_ArriveState()
        {
            return this.State_Arrive;
        }
        public int Get_LeaveState()
        {
            return this.State_Leave;
        }
        public float Get_Total_Correction_Dis()
        {
            return this.Act_Dis_Total_Correction;
        }

        public float Get_Act_Dis_Total_Correction_Old() 
        {
            return this.Act_Dis_Total_Correction_Old;
        }

        public float Get_Act_Dis_Total_Correction_New()
        {
            return this.Act_Dis_Total_Correction_New;
        }

        public float Get_Act_Distance_Total_Old()
        {
            return this.Act_Distance_Total_Old;
        }

        public float Get_Act_Distance_Total_New()
        {
            return this.Act_Distance_Total_New;
        }



        public bool Get_IsAuth()
        {
            return this.IsAuth;
        }
        public int Get_Act_Direction_Origin()
        {
            return this.Act_Direction_Origin;
        }
        public Dictionary<float, float[]> Get_Raise_Priority()
        {
            return this.Raise_Priority;
        }
    }
}
